Expert Spring MVC and Web Flow

(Dana P.) #1
Summary
In classic Spring Framework style, the MultiActionControllerlooks simple and straightforward,
but can be very configurable and flexible. It allows for one Controllerinstance to handle many
different requests by resolving the HTTP request into a method name. The method, itself a
request handler, is invoked via reflection, and its arguments are determined at runtime. The
arguments must include an HttpServletRequestand an HttpServletResponse, and the method
must return a ModelAndViewinstance. The method may also take either an HttpSessionobject or
a command bean of any type.
If you specify a command bean, it will be instantiated and populated with the request
parameters. You may specify one or more Validators, but if there are any errors there is no
easy way to get a reference to them. For this reason, if you wish to perform any data binding
and validation, we recommend you do this inside your request handling method. From there
you will be able to direct the request appropriately in the event of validation errors.
The strategy for method name resolution is provided by the interface MethodNameResolver,
and three different implementations are provided, each with their own pros and cons. The
default strategy is the InternalPathMethodNameResolver, simply converting the last path ele-
ment of the URI into a method name.
Finally, we can recommend the MultiActionControllerfor situations when you have a
logical grouping of read only requests, or for very simple POST requests. In either case, it helps
if all the requests have some common element, such as acting on the same type class or using
the same service object.

AbstractWizardFormController


The Controllers we’ve mentioned up to this point provide stateless request handling, with no
explicit features for a multipage work flow. However, there are many situations that call for an
ordered and consistent user experience that spans multiple pages.
For instance, the work flow for creating a new account might take two pages, with the
first page checking for username and email uniqueness and the second page accepting billing
details. Only after the user completes both pages, and no validation errors exist, will the sys-
tem create the account.
These multistep use cases, commonly called wizards, require complex state management
for the user experience. To help with this problem, we will use the org.springframework.web.
servlet.mvc.AbstractWizardFormController, a specialized Controllerproviding the basic
infrastructure for multiple form work flows. You should think of this Controlleras a
SimpleFormControllerthat has spread its form across multiple pages.

■Tip Explore the SimpleFormControllerbefore attempting to use AbstractWizardFormController,
as the two controllers treat forms in a similar manner.

176 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf