Announcement Announcement Module
No announcement yet.
How to apply HandlerInterceptor to a specific ModelAndView? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to apply HandlerInterceptor to a specific ModelAndView?

    Hi, recently I am trying to use postHandle method in HandlerInterceptor to clean attributes I put in session. My controller is MultiActionController. But the interceptor apply to all methods in controller. Is there any ways I can let it just apply to a specific method in controller? I noticed one of arguments in postHandle is ModelAndView. I set it up as the one to which I want to apply, but it still apply to all methods. Please help me out. Thanks. Following is my action-servlet.xml
    <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> 
          <property name="mappings"> 
              <prop key="/a.html">A_Action</prop> 
              <prop key="/b.html">B_Action</prop> 
              <prop key="/c.html">C_Action</prop> 
              <prop key="/d.html">D_Action</prop> 
    <bean id="urlMapping" 
          <property name="interceptor"> 
                <ref bean="interceptor"/> 
            <property name="mappings"> 
                                                      <prop key="/a.html">MyController</prop> 
               <prop key="/b.html">MyController</prop> 
               <prop key="/c.html">MyController</prop> 
               <prop key="/d.html">MyController</prop>
    Following is my postHandle
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import org.springframework.web.servlet.ModelAndView; 
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 
    public class temp extends HandlerInterceptorAdapter&#123; 
       public void postHandle&#40;HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView C_Action&#41; throws Exception &#123; 
    Based on example above, the interceptor apply to all methods, but I just want to apply it to C_Action.

  • cherryhill
    Good point. Thanks.

    Leave a comment:

  • cmgharris
    To do what you want, you will need to interrogate either the request object or your ModelAndView in the postHandle method, to see if the case is one for which you want to remove the attribute.
    If necessary, you could even add a request attribute or an item to your model to make that easier (though that's a bit ugly).

    I'm not sure, though, why you can't just remove the attribute in your controller?

    Leave a comment: