Expert Spring MVC and Web Flow

(Dana P.) #1
To be clear, the AbstractControllerdoes not implement the LastModifiedinterface. It is
up to you to provide this functionality, if your data supports it.
Cache control is a broad topic but, if implemented correctly, it can help to manage band-
width and CPU consumption considerably. For example, if the Controller’s main responsibility
is to pull static information from a database to be rendered in HTML, instructing clients to cache
the document will save repeated trips to the database, bandwidth, and processing load (always a
good thing).

Synchronized Session
The last bit of functionality the AbstractControllerwill perform on behalf of subclasses
is optionally synchronizing the handling of the request around the session object. By
setting synchronizeOnSessionto true, the Controllerwill synchronize every call to
handleRequestInternal()around the user’s HttpSessionobject. If there is no session,
then no synchronizing will take place no matter the value of synchronizeOnSession.
This is useful if the Controllermust ensure multiple requests from the same client are to
be handled in a serialized and nonconcurrent manner. When would you have to do this? If it is
possible to directly modify state that is stored in the session, it’s useful to serialize access to
the session in order to ensure that multiple processes don’t interfere with each other.
As browsers become more sophisticated and pre-fetch more pages (the Google Web
Accelerator does this, for example), the chances of web requests being performed in the
background increase. Be aware that the client may be creating multiple concurrent requests
without the user’s involvement. You may want to investigate the synchronizeOnSessionprop-
erty to protect your Controllers from such activity.

Summary


The AbstractControllerprovides the basis for all Controllerimplementations in Spring MVC.
It adheres to the Open-Closed Principle to protect its work flow and life cycle methods, while
providing a well-known extension hook in the form of handleRequestInternal(). The
AbstractControllerhandles session checking, HTTP method checking, cache control, and
synchronization, all configured by either the bean definition in the XML or through simple
setters called in the constructor.
For simple Controllerimplementations, subclass this class instead of merely implement-
ing Controller. For more robust work flows, we will now begin examining subclasses of
AbstractController.

BaseCommandController


While AbstractControlleris a good class for read-only web resources, an interactive web
application will require HTML forms support. Native servlet applications would require work-
ing directly with the HttpServletRequestand its getParameter()method to read submitted
form values. This method is error prone and cumbersome, as the developer needs to handle
missing values and potential type conversion. Modern web frameworks usually provide a
mechanism to automatically convert the raw request parameters into classes and properties,
so that the request handling code can interact with a strongly typed object instead of a collec-
tion of Strings.

122 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf