Expert Spring MVC and Web Flow

(Dana P.) #1

■Tip For the exact differences between 302 and 303 response codes, please consult the HTTP RFC at
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.The 303 response code was added to
HTTP/1.1 specifically for the redirect after post pattern, but is only understood by HTTP/1.1 clients. Luckily,
most HTTP/1.1 clients will treat a 302 response identically to a 303.


There are at least three different ways to accomplish a client redirect with Spring MVC
and the Servlet API. The first method, HttpServletResponse.sendRedirect(), uses the Servlet
API to correctly send the redirect response. You may use this method only if the response has
not been committed, however.
Spring MVC treats redirects as just another type of view with its org.springframework.
web.servlet.view.RedirectViewclass. This class encapsulates the redirect, the conversion of
model objects to query parameters, and the logic to handle a HTTP/1.0 or HTTP/1.1 redirect.
This view is then resolved like all other Views in the system, hiding the exact view details from
the controller.
While the RedirectViewis very simple to use, if your application is already using an
InternalResourceViewResolver, you will need to create another view resolver that is able to
resolve non-file system views. Because we already have an InternalResourceViewResolver
configured, we will need to chain the ViewrResolvers allowing each the chance to resolve the
view name. Once a resolver is able to handle the Viewname, resolving stops and the view
instance is returned.
To handle a RedirectView, we will define an XmlViewResolver(Listing 6-49), which
reads its list of view names from a Spring XML file. This ViewResolverwill be defined inside
spring-servlet.xml alongside the InternalResourceViewResolver. The XmlViewResolver,
however, will be configured to appear before the InternalResourceViewResolverin the chain,
so that it has a chance to resolve the view names first. We also specify where the resolver will
find its configuration (in this case, in /WEB-INF/views.xml).


Listing 6-49.Two View Resolvers, Chained


<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">






<bean id="auxViewResolver"
class="org.springframework.web.servlet.view.XmlViewResolver">





CHAPTER 6 ■THE CONTROLLER MENAGERIE 165
Free download pdf