• SpringMVC多拦截器的执行


    如果有多个拦截器,执行的顺序是在SpringMVC的配置文件里的前后顺序。

    <mvc:interceptors>
      <bean class="com.neuedu.interceptor.Inteceptor1"></bean>
      <mvc:interceptor>
        <mvc:mapping path="/test"/>
        <bean class="com.neuedu.interceptor.Inteceptor2"></bean>
      </mvc:interceptor>
    </mvc:interceptors>

    第一个拦截器在前面就先执行第一个拦截器的preHandle方法。

    第一个拦截器

    package com.neuedu.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    public class Inteceptor1 implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            // TODO Auto-generated method stub
            System.out.println("first-preHandle");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            System.out.println("first-postHandle");
            
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            System.out.println("first-afterCompletion");
            
        }
    
    }

    这样第二个拦截器:

    package com.neuedu.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    public class Inteceptor2 implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            // TODO Auto-generated method stub
            System.out.println("second-preHandle");
            return false;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            System.out.println("second-postHandle");
            
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            System.out.println("second-afterCompletion");
            
        }
    
    }

    请求的方法:

    @Controller
    public class BController {
        @RequestMapping(value="/test")
        public void test(){
    //        
            System.out.println("目标方法执行");
            
            
        }
    }

    执行的结果:

    first-preHandle
    second-preHandle
    目标方法执行
    second-postHandle
    first-postHandle
    second-afterCompletion
    first-afterCompletion

    结果说明多拦截器的顺序是:

     先执行第一拦截器的preHandle

    然后第二个拦截器的preHandle

    目标方法的方法

    第二个拦截器的postHandle

    第一个拦截器的postHandle

    第二个拦截器的afterCompletion

    第一个拦截器的afterCompletion

  • 相关阅读:
    C语言---堆的实现
    python的matplotlib---雷达图
    python的matplotlib饼状图
    python的matplotlib折线图
    python的matplotlib散点图学习
    python的matplotlib散点图
    C语言---队列(链表实现)
    hadoop集群启动与关闭需要输入密码
    hadoop集群启动时需要输入密码
    C语言---堆栈(链表实现)
  • 原文地址:https://www.cnblogs.com/xuesheng/p/7425874.html
Copyright © 2020-2023  润新知