Expert Spring MVC and Web Flow

(Dana P.) #1

Validation is done in piecemeal style as the user moves through the wizard, validating
only parts of the command bean on each step. At the last step in the wizard, all validations are
run again in order, effectively validating the complete command bean.
This controller should not be used for arbitrary work flows. Instead you should use Spring
Web Flow, which is capable of handling complex state changes and page flows.


ThrowawayController


There are at least two schools of thought on how to model a request/response-type web
framework. The first says that the request is something that should be passed around to
stateless handlers, which is exactly how servlets and Controllerswork. The second school
of thought says that the request should be modeled with the Command pattern and directly
executed. This matches how WebWork (http://www.opensymphony.com/webwork) has modeled
its request handling, for instance. If this is your cup of tea, Spring MVC provides an
org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerthat implements the
Command pattern for request handling.
Up to now, you’ve seen Controllers as stateless singletons in the system, working directly
with the HttpServletRequestand HttpServletResponseobjects in order to process requests.
The ThrowawayControllerprovides the alternative to this model because it encapsulates both
the state of the request as well as the behavior. The ThrowawayControlleris also a prototype
bean; a new instance is created for each request. For these reasons, ThrowawayControllersare
an entirely different breed of request handler, so much so that they don’t even implement the
Controllerinterface.


■Tip Controllers such as SimpleFormControllerdo not have to be singletons. You may configure any
controller type as a prototype if you wish, but ThrowawayControllermust be a prototype because its
design is not thread safe.


ThrowawayControllersare meant to act as the command bean and the request handler.
This controller literally adds an execute()method to a command bean, so that you may
directly execute it. Listing 6-74 contains the ThrowawayControllerinterface.


Listing 6-74.ThrowawayController Interface


public interface ThrowawayController {
ModelAndView execute() throws Exception;
}


The request parameters will be bound to your concrete subclass just like a command
bean. After binding, and assuming no errors were encountered, the execute()method will be
called.
Notice how this controller does not have access to any Servlet API classes, such as
ServletRequestor HttpSession. If you require these classes, you will need to use the
Controllerinterface.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 193
Free download pdf