Expert Spring MVC and Web Flow

(Dana P.) #1
■Tip We recommend that for configuration elements that will not change, such as the supported methods
for a Controller,the constructor is the best place to set them. The XML bean definitions are excellent
places for configuration elements that will change or are external to the definition of the bean itself.

How did Spring convert the string "GET,POST"into an array of Strings? Spring used the
org.springframework.beans.propertyeditors.StringArrayPropertyEditor, automatically
registered for ApplicationContexts. Learn more about PropertyEditors later in this chapter.

Require Sessions
The AbstractControllerwill also, if configured to do so, check for an existing HTTP
session in the request. If a session is not found, then the Controllerwill fail fast with a
SessionRequiredException. This may be useful when a Controlleris part of a work flow that
requires a session to already exist, as it should fail in a consistent manner and with a semanti-
cally rich exception if no session exists.
To configure this behavior, simply set the requiresSessionproperty to true. The default
behavior is to not require a session. Again, this can be done inside the constructor or in the
bean definition.

Cache Header Management
With a few simple configuration properties, the AbstractControllerwill also manage the
sending of cache control headers in the response. These headers will instruct the client, and
any caches between the client and server, on how to cache the document returned by the
Controller. By default, the Controllerwill not send any cache headers with the response.
To send cache control headers, simply set the cacheSecondsproperty to a value greater
or equal to zero. With zero as the value, the Controllerwill tell the client that no caching is
allowed. If the value is greater than zero, the Controllerwill send the appropriate headers to
indicate the content shall be cached for that many seconds. Of course, the client is free to
ignore any or all of these headers, so treat them as hints.

■Tip HTTP caching is a large concept with many nuances. Begin your research with the HTTP RFC’s
section on caching (http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13). Used
correctly, caching can save both bandwidth and system resources. At a minimum, enable caching for
read-only pages with static (or infrequently changing) content.

By default, both HTTP/1.0 and HTTP/1.1 cache manipulation headers will be sent by
AbstractController. Note that there are few clients out in the wild that speak only HTTP/1.0,
but sending both 1.0 and 1.1 headers is normally done to accommodate broken client imple-
mentations. For safety, and unless there is some other explicit reason, send both versions of
headers. To control sending of HTTP/1.0 headers, use the useExpiresHeaderproperty. To
control sending of HTTP/1.1 headers, use the useCacheControlHeaderproperty.

120 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf