Expert Spring MVC and Web Flow

(Dana P.) #1

The org.springframework.web.servlet.mvc.BaseCommandControllerclass provides the
basic feature set for supporting form submits, including creating JavaBeans from the request,
and registering Validators. It does not support the notion of a page view work flow, nor does
it do anything with the JavaBean once created. This class is the parent for classes such as
SimpleFormControllerthat build upon its functionality to bring coordinated page views to
the user.


■Tip If you are looking to handle form submits, look past the BaseCommandControllerto
SimpleFormController.BaseCommandControllerdoes not provide any work flows that you can
extend easily, but it does provide much of the base functionality.


BaseCommandControllersubclasses AbstractControllerand provides the concept of a
command object. A command, in this scenario, is a JavaBean whose properties are set from
HTTP request parameters.


■NoteDo not confuse a command bean with the Gang of Four’s Command design pattern. The formal
Command pattern implies the object has a well-known execution interface (some sort of execute()
method, for instance), encapsulating a callback. In contrast, the BaseCommandControllerdoes not call any
methods on its command object once it is created. Of course, you could extend BaseCommandController
and implement the Command pattern (or simply use ThrowawayController), but know that there is no
such built-in callback work flow in this class.


One important note about BaseCommandControlleris that it does not, itself, define any
work flow. That is, while it provides functionality such as binding request parameters to beans
and life cycle methods for validation, it does notput them together to create anything mean-
ingful. Its subclasses, such as AbstractFormControllerand SimpleFormController, will add
the value.
Our interest in this class is to explain how Spring MVC converts, or binds, request param-
eters to JavaBean properties. This feature is not unique, as many web frameworks have been
doing this for years. Spring MVC’s benefit is that it does not force a particular type, or super-
class, for the command class. You are free to use any class that conforms to the JavaBeans
model, and this freedom can lead to significantly fewer classes in your system. This means
that you will be able to populate domain classes directly from requests, removing the need for
otherwise duplicate form classes.
Before we show you how to work with a freshly bound command object, we will first cover
the capabilities and limitations of populating beans from HTML form submits, also known as
data binding. Because the BaseCommandControllerdoesn’t have any work flow, when we talk
about form processing, we will introduce the SimpleFormController. For now, we present data
binding for beans. Note that while Spring MVC takes advantage of data binding, the binding
framework is not web-specific and can be used with ease outside of the web framework.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 123
Free download pdf