码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc--拦截器的preHandler返回false后的执行顺序

时间:2017-03-15 14:25:44      阅读:1518      评论:0      收藏:0      [点我收藏+]

标签:get   patch   pid   frame   nts   分享   path   plugin   mapper   

编写springmvc中的自定义拦截器

1.实现接口 HandlerInterceptor

package com.kevin.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyHandlerInterceptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------------------preHandle--------------------------");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        
        System.out.println("------------------postHandle--------------------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("------------------afterCompletion--------------------------");
        
    }

}
package com.kevin.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyHandlerInterceptor2 implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------------------preHandle2--------------------------");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        
        System.out.println("------------------postHandle2--------------------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("------------------afterCompletion2--------------------------");
        
    }

}

2.在springmvc-servlet.xml的配置文件中加入拦截器配置

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.kevin.interceptors.MyHandlerInterceptor"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.kevin.interceptors.MyHandlerInterceptor2"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

3.controller

@Controller
@RequestMapping("/demo")
public class AnnotationController {

    /**
     * test annotation springmvc
     * @return
     */
    @RequestMapping("anno")
    public ModelAndView testAnnotation(){
        ModelAndView mv = new ModelAndView("anno");
        mv.addObject("msg", "注解驱动的mvc");
        return mv;
    }
    
}    

4.我的tomcat配置

<build>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>${tomcat-maven-plugin.version}</version>
                <!-- <tomcat-maven-plugin.version>2.2</tomcat-maven-plugin.version> -->
                <configuration>
                    <port>80</port>
                    <!-- http://localhost:port/path  使用80端口浏览器输入地址可省略-->
                    <path>/</path>
                    <!-- <path>/my-springmvc</path> -->
                </configuration>
            </plugin>
        </plugins>
    </build>

5.debug启动项目,

在springmvc入口 DispatcherServlet 中加入断点

chrome浏览器  http://localhost/demo/anno.do

 

技术分享

 

getHandler 方法查找mapper  ,返回handler对象和拦截器数组

技术分享

执行拦截器前置方法

技术分享

进入方法内

技术分享

 

由于 MyHandlerInterceptor2 的 preHandle 方法返回的是 false,执行到拦截器2的preHandler后会进入中间135行的triggerAfterCompletion 方法

此时控制台打印

------------------preHandle--------------------------
------------------preHandle2--------------------------

this.interceptorIndex 的值为1

进入triggerAfterCompletion方法

技术分享

最终结果为

------------------preHandle--------------------------
------------------preHandle2--------------------------
------------------afterCompletion--------------------------

6.如果将MyHandlerInterceptor2的preHandler的返回值改为true,结果是正常的拦截器执行过程

------------------preHandle--------------------------
------------------preHandle2--------------------------
------------------postHandle2--------------------------
------------------postHandle--------------------------
------------------afterCompletion2--------------------------
------------------afterCompletion--------------------------

 

springmvc--拦截器的preHandler返回false后的执行顺序

标签:get   patch   pid   frame   nts   分享   path   plugin   mapper   

原文地址:http://www.cnblogs.com/gavin2016/p/6553880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!