Expert Spring MVC and Web Flow

(Dana P.) #1

You can also bind to properties of objects that live inside collections, such as Sets, Insert
arrays, after Lists, and Maps. You can bind directly to objects that live in Lists, arraysor Maps
(but not Sets, as there is no way to set a member of a Setdirectly).
We recommend that you control which properties can be bound to, to guard against mali-
cious binding attempts. Using the DataBinder’s setAllowedFields()method, you can declare
the names of properties to be bound to request parameters. If the request parameter is not in
that list, it will be silently dropped and will not be set into the bean.
An error will be generated when a request parameter cannot be converted into the field’s
type. For instance, if a String value is given to a field expecting an integer, a FieldErrorwill
be created and stored inside the BindExceptionobject. This functionality happens without any
explicit configuration on the DataBinder.
Data binding is provided for you via the BaseCommandControllerclass, which knows only
how to bind request parameters to command classes. Subclasses of this class build upon this
base functionality to create cohesive work flows.
The command classes, encapsulating form submissions, do not require a special class
type; the DataBinderwill happily bind to any class that obeys JavaBean conventions with
standard getters and setters. We encourage you to use your domain model objects as the
command classes and populate them directly from form submissions.
Now that we have thoroughly covered data binding, it’s time now to look at the
SimpleFormControllerclass. This class builds upon the BaseCommandControllerto provide a
very full-featured work flow for HTML forms. You will see how to apply your new data binding
skills and how to process a command class once it has been populated by form fields.


SimpleFormController and Handling Forms


The org.springframework.web.servlet.mvc.SimpleFormControlleris a very powerful
Controllerresponsible for handling the entire life cycle of HTML form interaction. This
Controllermanages and coordinates viewing the form, through validation, and finally to
handling the submission. If your page or resource does not have to handle any form submits,
you can move up the class hierarchy to use a simpler controller such as AbstractController.
If you need to display a form and handle its submission, then this is the Controllerfor you.


■Tip This controller extends AbstractController,so it inherits all of the work flow from
AbstractController.It does not attempt to change or replace the logic of AbstractController.


One very nice aspect of this class is that it models the entire life cycle of form interaction.
It handles all the details and provides very explicit extension points allowing you to append
functionality during the process of form viewing or submission.
SimpleFormControlleris configured through many different properties (listed in Table 6-2).
Because this class attempts to obey the Open-Closed Principle, the properties are provided to
declaratively configure the work flow and behavior of the controller.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 149
Free download pdf