Expert Spring MVC and Web Flow

(Dana P.) #1

The default implementation of this interface is the org.springframework.web.servlet.
handler.SimpleMappingExceptionResolver. This class maps exceptions to view names by the
exception class name or a substring of the class name. This implementation can be configured
for individual Controllers or for globally for all handlers. The example configuration in
Listing 5-19 illustrates these options.


Listing 5-19.Example SimpleMappingExceptionResolver ApplicationContext


<?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">


appErrorView
someErrorView
genericErrorView




The exceptionMappingsproperty is a java.util.Propertieswith substrings (explained
later in this section) of exception class names as keys and Viewnames as values. Notice how
you can specify a fully qualified class name or only part of a class name for the key. The error
Viewname will be ultimately resolved by a ViewResolver.
Listing 5-19 does not specify a particular request handler, so it will be applied to any
mapped exception by any handler. However, you can bind an exception resolver to specific
handlers to create very specific mappings. Use this technique when you require displaying dif-
ferent error pages for the same exception thrown by two different controllers.


■CautionMapping exceptions to individual handlers only works if the handler is a singleton. Unless you
are using ThrowawayControllers,this should not be an issue because normally Controllers are single-
tons. However, it is always possible to run any controller as a prototype.


Listing 5-20 provides two examples, one for mapping an exception resolver to a single
request handler, and the other for mapping to multiple request handlers.


Listing 5-20.Two Exception Resolvers for Specific Handlers


<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC


CHAPTER 5 ■THE PROCESSING PIPELINE 95
Free download pdf