Expert Spring MVC and Web Flow

(Dana P.) #1

■Tip This technique is nice because it fails fast and can protect the semantics of your request handling
method. Plus, it generates the appropriate HTTP error code (405) so that clients know exactly what the
error was.


A second strength you can see is that MultiActionControllersupports binding the
request to a command bean. For this example each request is using the same bean class,
SearchCriteria, but in fact each request handling method can use a different command bean
class. We are using a static inner class for convenience, but any type of JavaBean can be used.
The MultiActionControllerwill, by default, create an instance of the specified command
bean by simply calling Class.newInstance(). A new instance of the command object is created
for every request. If you wish to change this behavior, override newCommandObject()in your
subclass. Options for alternate object creation include pulling an instance from a BeanFactory
or using method injection to transparently return a new instance.
A downside of using command beans is that, as we’ve mentioned before, the
MultiActionControllerdoesn’t provide a form handling work flow. This means that if there
is a data binding error, there is no way to trap that exception inside the controller itself. The
exception handling facilities alluded to earlier only apply to exceptions thrown from inside the
request handling methods. Any data binding exceptions, or errors generated from validation,
are thrown outside the class. From there you can use the DispatcherServlet’s exception map-
ping and handling facilities, but this can become complicated. Our advice is to simply not
attempt to emulate a form handling, or intelligent command data binding, work flow with
MultiActionController. It is because of this that you do not see the use of Validators in this
example.
However, this controller does provide exception handling capabilities, useful for
business logic exceptions. We will now append our example (shown in Listing 6-62) to
include the handling of a AccountNotFoundException, which can be thrown by AccountService.
findAccountByUsername()in the event that the provided username did not locate an account.


Listing 6-62.Handling the AccountNotFoundException


public ModelAndView accountNotFound(HttpServletRequest request,
HttpServletResponse response, AccountNotFoundException e) {
List errorMessages = new ArrayList();
errorMessages.add("No account found for " +
request.getParameter("searchBy"));
return new ModelAndView("accountFindError", "errorMessages",
errorMessages);
}


The name of the method, in this case accountNotFound, has no bearing on the work flow
involved. The exception handling method must, however, return ModelAndViewand have three
parameters: HttpServletRequest, HttpServletResponse, and an instance of Throwable.
If no exception handling method is found for a type of exception, a method for its super-
class will be searched for, until the class Throwableis encountered. This allows you to write
one exception handling method to encompass an entire exception class hierarchy.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 175
Free download pdf