Chapter 4. Basic web forms


This chapter covers

  • Building a web form
  • Externalizing strings in a view
  • Validating and saving form data

Web forms provide a means by which we can collect data from end users. As such, they’re a key aspect of any nontrivial web application. This chapter shows how to use Spring Web MVC and related technologies to build a simple user registration form with standard features such as redirect-after-post, externalized strings, form data validation, and persistence.

Our approach is hands-on and practical. See Spring in Action, 3rd edition by Craig Walls (Manning, 2011) for additional material on Spring Web MVC.

4.1. Displaying a web form



Key technologies

Spring Web MVC, Spring form tag library


Users establish a relationship with a website or an organization by registering. The resulting user account allows logins, order placement, community participation, and so on. The first step in supporting a user registration process is to display a registration form.


Create a web-based form.


In this recipe you’ll use Spring Web MVC to display a user registration form. You’ll build a user account form bean, a web controller, a registration form, and a confirmation page.

It won’t hurt to have a visual on the UI you’re planning to create in this recipe. Figure 4.1 shows what you’re aiming for.

Figure 4.1. The simple web-based registration form that you’ll build in this recipe

Let’s begin by creating a form bean for your user accounts.

4.2. Externalizing strings in the view

4.3. Validating form data

4.4. Saving form data

4.5. Summary