gnreddy
gnreddy

Reputation: 2503

Spring 3.0 MVC Handler Interceptors not working

I am trying out the HandlerInterceptors from Spring MVC 3.0.

Below is my interceptor

public class SessionInterceptor extends HandlerInterceptorAdapter {

   @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      System.out.println("inside preHandle");
      if(request.getSession().getAttribute(SessionConsta nts.USER_SESSION_NAME) == null) {
         response.sendRedirect("welcome");
         return false;
      }
      return true;
   }
}

Below is my configuration in my xml

<mvc:annotation-driven/>

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/services/*"/>
<bean class="com.ca.myca.interceptors.SessionInterceptor " />
</mvc:interceptor>
</mvc:interceptors>

But the interceptor is not getting called.

Please let me know if I am missing any thing.

Upvotes: 0

Views: 5310

Answers (4)

ledlogic
ledlogic

Reputation: 850

In reference to the post above by arviarya, <mvc:annotation-driven /> in the config XML results in a different handler Object being passed to the interceptor. In our interceptor method we had:

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) throws Exception {
    if (mav != null && handler instanceof HandlerMethod) {
        // something we want to have happen
    }

This was being called with the @Controller-derived object without the <mvc:annotation-driven />, but was called with the HandlerMethod-derivedobject when it was present. For our if block to work, I needed the tag in our config XML.

Upvotes: 0

Sheetal Mohan Sharma
Sheetal Mohan Sharma

Reputation: 2924

try what is suggested in Configuration of Spring MVC and JSON using Jackson.

Put you interceptor in <mvc:interceptors> tag

<mvc:interceptors>
    <bean class="xx.x..x..x...UserSessionInterceptor" />
</mvc:interceptors>

you can keep <mvc:annotation-driven/> and <context:annotation-config>

Upvotes: 0

arviarya
arviarya

Reputation: 660

You are using <mvc:annotation-driven/> with mvc interceptor.

Please check on Spring reference:

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/DispatcherServlet.html

"When running in a Java 5+ environment, a default AnnotationMethodHandlerAdapter will be registered as well. HandlerAdapter objects can be added as beans in the application context, overriding the default HandlerAdapters. Like HandlerMappings, HandlerAdapters can be given any bean name (they are tested by type)."

<mvc:annotation-driven/> is supposed to be used for annotation-driven MVC controllers like @RequestMapping, @Controller etc, but I have seen there is no need to define "<mvc:annotation-driven/>" for supporting it.

Unless you are using jackson (for json support), you can try to remove <mvc:annotation-driven/> and use "<context:annotation-config>" instead for common use like autowiring etc.

Upvotes: 1

Nobwyn
Nobwyn

Reputation: 8685

In our application we are using double ** for any service sub-path match, so try changing it and check if it helps:

<mvc:mapping path="/services/**"/>

Upvotes: 1

Related Questions