Expert Spring MVC and Web Flow

(Dana P.) #1
Summary
The ThrowawayControlleris an alternate request handling method compared to the
Controllers we’ve seen so far. It is intended to encapsulate both the request parameters as
well as the behavior associated with the request. A new ThrowawayControlleris created for
each request, so it must be a prototype bean (by specifying singleton="false"in the bean
definition).
Request parameters are bound directly to the controller, and if there are no data binding
errors, the controller’s execute()method is called to handle the request.

ValidatableThrowawayController


The standard ThrowawayControllercan’t support any fine grained data binding configuration
because there is no callback to specify any custom PropertyEditors. If there are any data bind-
ing errors, the controller never knows about them, making proper error handling cumbersome.
Enter the ValidatableThrowawayController, as shown in Listing 6-77, which adds a bit
more complexity but fills in the gaps of error handling. This controller type is still a stateless
Command pattern implementation of a controller, so you should use it wherever you would
use a ThrowawayControllerbut require the ability to register custom PropertyEditors or build
work flows that take into account any errors.

Listing 6-77.ValidatableThrowawayController

public interface ValidatableThrowawayController {

String getName();
void initBinder(DataBinder binder) throws Exception;
ModelAndView execute(BindException errors) throws Exception;

}

If you wish to use this controller, you must also declare a ValidatableThrowaway➥
ControllerHandlerAdapterin your WebApplicationContext. If you do, be sure to also include
any other handler adapters, as the defaults are only included if no handler adapter is found in
the ApplicationContext.

HandlerInterceptors


The Servlet 2.3 specification introduced the idea of filters, common code that can wrap one or
more servlets to provide pre- and post-processing of the request and response. Spring MVC
supports an analogous concept with its HandlerInterceptors, which wrap request handlers to
provide common functionality. Interceptors handle more life cycle events than a standard fil-
ter, but filters are more powerful, in that they may directly manipulate or replace the
HttpServletRequestand HttpServletResponseobjects.
Listing 6-78 contains the HandlerInterceptorinterface.

196 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf