Expert Spring MVC and Web Flow

(Dana P.) #1

And finally, we will add a Controllerto handle the new success page. Why do we need a
Controllerfor a simple JSP page? A Controllercan hide the implementation of the Viewfrom
the client, and it provides a uniform URL address space. For consistency’s sake, it’s a good idea
to front even simple Views with a Controller. As you’ll see, Spring MVC make this a straight-
forward process.
The view name newPersonSuccessis now being used for the redirect view, which
redirects to the controller identified by /personSuccess. In this case, we simply wish to
display the success message so we aren’t interested in coding up a Controllerjust to forward
to a JSP page. Luckily, Spring MVC provides an org.springframework.web.servlet.mvc.
UrlFilenameViewControllerthat can convert the last part of the request URL to a view name.
This avoids the need to write a custom Controllerfor resources that are only implemented
as Views.
For instance, given the URL /app/address.x, the UrlFilenameViewController will convert
the URL into the view name address. This is a very easy way to expose Viewresources such as
JSP pages while continuing to hide their implementation technology.


■Tip For resources that are only views, hide them behind simple controllers such as
UrlFilenameViewControlleror ParameterizableViewController.This hides implementation
revealing clues, such as the .jsp extension, and can provide a uniform URL address space. This also allows
you to place your view files in the protected /WEB-INFdirectory.


For the example, we now add the bean definition for the /personSuccessresource (shown
in Listing 6-53), which the client will be redirected to upon successful form submission. This
definition should go in spring-servlet.xmlwith the other web-specific beans.


Listing 6-53./personSuccess Bean Definition


<bean name="/personSuccess"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />


With this last change, the Controllernow correctly redirects the client after the form
submission. We accomplished this by using a RedirectViewand an XmlViewResolver, which is
a good strategy when you wish to keep the Controllerunaware of the implementations of any
views.
However, quite a bit of infrastructure was required for this configuration. Spring MVC pro-
vides shorthand for redirect views, allowing you to forgo the definition of any RedirectViewsor
the configuration of an explicit XmlViewResolver. The UrlBasedViewResolver(the superclass for
InternalResourceViewResolver) recognizes the special prefix redirect:, which simply triggers a
client redirect instead of being resolved through the standard process.
Converting the PersonFormControllerto use this shorthand is quite easy, and involves
mostly deleting code we just created. The Controller’s success view name will now be changed
to use the redirect:prefix (as shown in Listing 6-54), alleviating the need for a RedirectView
definition or an XmlViewResolverdefinition.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 167
Free download pdf