Expert Spring MVC and Web Flow

(Dana P.) #1

The Controller Menagerie


AControlleris the workhorse of Spring MVC, providing the glue between the core applica-
tion and the web. We’ve mentioned Controllers several times up to this point, and we will
now provide an in-depth review of the different available Controllerimplementations.
This chapter will also cover many of the details surrounding form submission, including
details on binding form data to POJOs. Related to data binding are simple validation and how
PropertyEditors help convert Strings to complex types. We’ll cover both in this chapter.


Introduction


It is important to note that a Controllerin Spring MVC is not the same thing as a Front Con-
troller. Martin Fowler in Patterns of Enterprise Application Architecture(Addison Wesley, 2002)
defines a Front Controller as “a controller that handles all requests for a Web site.” By that defi-
nition, the DispatcherServletserves the role of a Front Controller. A Spring MVC Controller
is really a Page Controller, which Fowler defines as “an object that handles a request for a spe-
cific page or action on a Web site.”
In Spring MVC there are two high-level types of Controllers: Controllers and
ThrowawayControllers. Controllers are (typically) singleton, multithreaded page controllers
fully aware of the Servlet API (e.g., HttpServletRequest, HttpServletResponse, and so on).
A ThrowawayControlleris an executable command object (providing an execute()method)
that is populated directly with request parameters and has no awareness of the Servlet API.
A ThrowawayControlleris not intended for multithreaded use, but instead for one-off execu-
tion (hence its name).


■TipAdmittedly the naming convention leads you to believe a ThrowawayControlleris a subclass of
Controller,but in fact a ThrowawayControllerworks very differently than a Controller,and it is not
a subclass of Controller.We will discuss both types in this section, but be aware that a Controlleris
treated differently than a ThrowawayController.


115

CHAPTER 6


■ ■ ■

Free download pdf