Results 1 to 3 of 3

Thread: How to apply HandlerInterceptor to a specific ModelAndView?

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    4

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    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?
    Chris Harris
    Carlisle, UK

  3. #3
    Join Date
    Sep 2005
    Posts
    4

    Default

    Good point. Thanks.

Similar Threads

  1. RFE? Create fixed ModelAndView in XML
    By wangjammer5 in forum Container
    Replies: 4
    Last Post: Oct 5th, 2005, 09:07 AM
  2. PerformanceMonitorInterceptor
    By tnist in forum AOP
    Replies: 3
    Last Post: Aug 24th, 2005, 01:39 PM
  3. Replies: 8
    Last Post: Jul 26th, 2005, 07:33 AM
  4. Replies: 1
    Last Post: Feb 24th, 2005, 03:18 PM
  5. Apply fonts dynamically
    By DrTronic in forum Swing
    Replies: 2
    Last Post: Jan 6th, 2005, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •