首页 > 代码库 > springmvc--拦截器的preHandler返回false后的执行顺序
springmvc--拦截器的preHandler返回false后的执行顺序
编写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后的执行顺序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。