Announcement Announcement Module
Collapse
No announcement yet.
How to apply HandlerInterceptor to a specific ModelAndView? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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
    Code:
    <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> 
          <property name="mappings"> 
            <props> 
              <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> 
             </props> 
          </property> 
        </bean> 
    
    <bean id="urlMapping" 
            class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
          <property name="interceptor"> 
             <list>          
                <ref bean="interceptor"/> 
             </list> 
          </property> 
            <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
    Code:
    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; 
          request.getSession&#40;&#41;.removeAttribute&#40;"map"&#41;; 
       &#125; 
    &#125;
    Based on example above, the interceptor apply to all methods, but I just want to apply it to C_Action.

  • #2
    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?

    Comment


    • #3
      Good point. Thanks.

      Comment

      Working...
      X