Expert Spring MVC and Web Flow

(Dana P.) #1
The first thing to note about SimpleFormControlleris that it is intended to handle both
the viewing of the form and the submission of the form. That is, this Controllerwill respond
to both an initial HTTP GET request for the form itself, as well as the HTTP POST request
when the form is submitted.

Command Bean
The SimpleFormControllercreates and populates a command bean, which contains all the
parameters from the form as JavaBean properties accessible via getters and setters. The com-
mand bean will be validated if validation is enabled, and if no errors are found, the command
bean will be processed. Otherwise, the original form page will be shown again, this time with
the errors from validation.

■Tip For more on validation, consult Chapter 9.


Command beans can be any POJO that implements JavaBean-style getters and setters,
and they are not required to subclass any framework-specific class or interface. Also of benefit
is that Spring MVC allows for the properties of the command class to be of any type or class.
Properties of command beans can be simple types (such as intor boolean), simple classes
(such as Stringor Long), or even complex classes (such as Date).
Spring will use PropertyEditors, which come from the JavaBean specification, to provide
the conversion between request parameters (which are only Strings) and the specific type
required by the command class. For instance, we will use a CustomDateEditor, which can con-
vert strings such as “2005-06-30” to java.util.Dateinstances, when we are populating
SearchFlights’ Dateproperties.
The framework ships with many useful PropertyEditors, and it is easy to create your own
for your own custom types and classes. We will cover the full range of PropertyEditors when
we cover Controllers in Chapter 6.

Binding to a Form
On a form submission, the properties of the command bean are populated via JavaBean-
compatible getters and setters. When the SimpleFormControllerencounters a request parame-
ter, it will use the parameter name to identify which command bean property to set with the
value.
For instance, we will use the SearchFlightsclass for the command bean for this use case,
and it has a property named departFrom. When creating an XHTML text input element, the
name of the corresponding form field must also be departFrom. Refer to Listing 4-15.

Listing 4-15.departFrom Text Input
<input type="text" name="departFrom" />

■NoteConvenience tags are available to help with rendering form elements for some view technologies.
Consult Chapters 7 and 8 for details.

66 CHAPTER 4 ■JUMP INTO SPRING MVC

Free download pdf