Expert Spring MVC and Web Flow

(Dana P.) #1
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// nothing
}

}

HandlerMappinginstances create a HandlerChain, combining HandlerInterceptors with
the request handler such as a Controller. Therefore, HandlerInterceptors much be bound to
HandlerMappings.
If you want the interceptor to apply to all request handlers and you are using only the
BeanNameUrlHandlerMappingobject, then the configuration is fairly straightforward. Listing
6-80 contains an example configuration and definition for the DateInsertionInterceptor.

Listing 6-80.HandlerInterceptor Configuration

<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<bean
class="com.apress.expertspringmvc.flight.web.DateInsertionInterceptor" />
</list>
</property>
</bean>

The above configuration is more verbose than normal, as now you must declare the
BeanNameUrlHandlerMapping, which previously was the implicit default.

■NoteHere we are taking advantage of Spring’s support for inner bean definitions, useful in this context
because the interceptor bean is only needed inside this HandlerMapping.

The only way to configure which interceptors will handle each URI is to bind them
to the appropriate HandlerMappinginstance. This might mean you need to create more
HandlerMappingobjects just to handle the way you would like the interceptors to be handled.

Summary


HandlerInterceptors are an excellent opportunity to apply common business logic to many
Controllers. They act much like filters, in that they intercept the request handling pipeline.
They are capable of bypassing the request handling altogether, placing common objects into
the model, and cleaning up resources after every request.
Interceptors are bound to HandlerMappingobjects, so any request that the handler map-
ping can handle will be routed through all of its interceptors and a single request handler.

198 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf