Spring Book – Chapter 14 – Spring MVC and Spring Web Flow

Handler Interceptors

Spring MVC gives you option to do something or rather give you a handle by which to control the action before the controller is called, after it is called and after completion of flowing of control from the view to the DispatcherServlet in the form of Handler Interceptors.

It can be considered as very much similar to AOP but more to do with Spring MVC controllers. Listing 14-12 below shows the definition of HandlerInterceptor interface.

Listing 14-12. HandlerInterceptor interface definition

Figure 14-10 below shows calling of HandlerInterceptor methods at various stages in a typical Spring MVC request flow.

HandlerInterceptor method invocation in Spring MVC request flow

Figure 14-10. HandlerInterceptor method invocation in Spring MVC request flow

Before going into the configuration details of HandlerInterceptor, below summarized are some of the uses of this:

  • Can do functionalities which are considered common to many controllers
  • Auditing requests or as a matter of fact anything considered important
  • Setting of response headers if necessary
  • Measuring performance and logging in performance quantitatively
  • Adding common attributes to models

While declaring your HandlerMapping, you can define your HandlerInterceptor as shown in Listing 14-13 below.

Listing 14-13. Declaring HandlerInterceptor while declaring HandlerMapping

Spring 3.0 introduces some very good configuration simplifications to configuring HandlerInterceptors as shown in Listing 14-14 below in which these interceotrs will be applied to all HandlerMapping.

Listing 14-14. Simplification of HandlerInterceptor configuration – Applies to all HandlerMapping

Page Visitors: 8091

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *