Expert Spring MVC and Web Flow

(Dana P.) #1

Listing 5-27 contains a simple mapping with both ExceptionParentand ExceptionChild.
Which one will resolve is based on which exception is thrown. Listing 5-28 shows that when
throwing ExceptionChild, the view name childPageresolves because ExceptionChildis more
specific than ExceptionParent.


Listing 5-27.Exception Resolver Mapping Both ExceptionParent and ExceptionChild


<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">



class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">


parentPage
childPage




Listing 5-28.Test Case


ModelAndView mav = resolver.resolveException(req, res, handler,
new ExceptionChild()); // throwing child subclass
assertEquals("childPage", mav.getViewName()); // true!


Of course, if you configured that last exception resolver with a mapping of Exce, then that
would take precedence over either previous mapping.


Summary


To summarize, the HandlerExceptionResolverinterface provides a mechanism to centralize
exception handling and remove it from the primary work flow logic. You can configure multi-
ple exception resolvers in an ApplicationContext, and they can be ordered by priority.
Spring provides a single implementation of this interface called
SimpleMappingExceptionResolverthat maps exception names to error pages. This implemen-
tation can match full class names or substrings, will prefer a shorter name, and is aware of the
class hierarchy when attempting to match the exception.
The DispatcherServletis aware of all exception resolvers in the ApplicationContext
and can order them based on priority. You can change this behavior by setting the
DispatcherServlet’s detectAllHandlerExceptionResolversproperty to false, in which case
you will need to define a single exception resolver with the name handlerExceptionResolver.


CHAPTER 5 ■THE PROCESSING PIPELINE 99
Free download pdf