美文网首页ssm
SpringMVC 拦截器<6>

SpringMVC 拦截器<6>

作者: 天空在微笑 | 来源:发表于2017-08-17 23:23 被阅读6次

    servlet/filter/listener/interceptor区别与联系

    1. 创建Interceptor
    package com.company.combine.intercepter;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Created by liuqun on 2017/8/17.
     */
    public class MyCustomerIntercepter1 implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            System.out.println(getClass().getName()+"  preHandle");
    
    
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            System.out.println(getClass().getName()+"  postHandle");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            System.out.println(getClass().getName()+"  afterCompletion");
        }
    }
    
    package com.company.combine.intercepter;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Created by liuqun on 2017/8/17.
     */
    public class MyCustomerIntercepter2 implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            System.out.println(getClass().getName()+"  preHandle");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            System.out.println(getClass().getName()+"  postHandle");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            System.out.println(getClass().getName()+"  afterCompletion");
        }
    }
    
    1. 在配置文件中配置:
     <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <bean class="com.company.combine.intercepter.MyCustomerIntercepter1"/>
            </mvc:interceptor>
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <bean class="com.company.combine.intercepter.MyCustomerIntercepter2"/>
            </mvc:interceptor>
        </mvc:interceptors>
    
    1. preHandle返回false则打断拦截链直接返回,后续不再执行。返回true,则继续。
      3.1两个HandlerInterceptor的preHandle()方法都返回true的执行结果
    com.company.combine.intercepter.MyCustomerIntercepter1  preHandle
    com.company.combine.intercepter.MyCustomerIntercepter2  preHandle
    com.company.combine.intercepter.MyCustomerIntercepter2  postHandle
    com.company.combine.intercepter.MyCustomerIntercepter1  postHandle
    com.company.combine.intercepter.MyCustomerIntercepter2  afterCompletion
    com.company.combine.intercepter.MyCustomerIntercepter1  afterCompletion
    

    3.2 MyCustomerIntercepter2的preHandle返回false执行结果

    com.company.combine.intercepter.MyCustomerIntercepter1  preHandle
    com.company.combine.intercepter.MyCustomerIntercepter2  preHandle
    com.company.combine.intercepter.MyCustomerIntercepter1  afterCompletion
    

    3.3 两个Intercepter的preHandle都返回false执行结果

    com.company.combine.intercepter.MyCustomerIntercepter1  preHandle
    

    相关文章

      网友评论

        本文标题:SpringMVC 拦截器<6>

        本文链接:https://www.haomeiwen.com/subject/lzqorxtx.html