标签:title int net 日志信息 fill 接口 exce mode nbsp
一、正常流程下的拦截器(全部放行)
1.springMVC中拦截器实现这个接口HandlerInterceptor
第一个拦截器 HandlerInterceptor1
-
public class HandlerInterceptor1 implements HandlerInterceptor {
-
-
-
-
-
public boolean preHandle(HttpServletRequest request,
-
HttpServletResponse response, Object handler) throws Exception {
-
System.out.println("HandlerInterceptor1...preHandle");
-
-
-
-
-
-
-
-
public void postHandle(HttpServletRequest request,
-
HttpServletResponse response, Object handler,
-
ModelAndView modelAndView) throws Exception {
-
System.out.println("HandlerInterceptor1...postHandle");
-
-
-
-
-
public void afterCompletion(HttpServletRequest request,
-
HttpServletResponse response, Object handler, Exception ex)
-
-
System.out.println("HandlerInterceptor1...afterCompletion");
-
-
第二个拦截器 HandlerInterceptor2
-
public class HandlerInterceptor2 implements HandlerInterceptor {
-
-
-
-
-
public boolean preHandle(HttpServletRequest request,
-
HttpServletResponse response, Object handler) throws Exception {
-
System.out.println("HandlerInterceptor2...preHandle");
-
-
-
-
-
-
-
-
public void postHandle(HttpServletRequest request,
-
HttpServletResponse response, Object handler,
-
ModelAndView modelAndView) throws Exception {
-
System.out.println("HandlerInterceptor2...postHandle");
-
-
-
-
-
public void afterCompletion(HttpServletRequest request,
-
HttpServletResponse response, Object handler, Exception ex)
-
-
System.out.println("HandlerInterceptor2...afterCompletion");
-
-
springMVC.xml中配置两个拦截器
-
-
-
-
-
-
-
-
-
-
-
-
-
查看打印的日志信息
HandlerInterceptor1...preHandle
HandlerInterceptor2...preHandle
HandlerInterceptor2...postHandle
HandlerInterceptor1...postHandle
HandlerInterceptor2...afterCompletion
HandlerInterceptor1...afterCompletion
这是正常的拦截器执行流程
以上执行的拦截器是正常流程的拦截器的顺序
二、非正常情况下的拦截器(第一个拦截器放行,第二个拦截器拦截)
细心的读者会发现在拦截器中preHandle方法中的返回值我做了注解, //return false表示拦截,不向下执行//return true表示放行
-
public class HandlerInterceptor2 implements HandlerInterceptor {
-
-
-
-
-
public boolean preHandle(HttpServletRequest request,
-
HttpServletResponse response, Object handler) throws Exception {
-
System.out.println("HandlerInterceptor1...preHandle");
-
-
-
-
再次启动系统拦截器的执行顺序
日志打印
HandlerInterceptor1...preHandle
HandlerInterceptor2...preHandle
HandlerInterceptor1...afterCompletion
总结:
拦截器1放行,拦截器2 preHandle才会执行。
拦截器2 preHandle不放行,拦截器2 postHandle和afterCompletion不会执行。
只要有一个拦截器不放行,postHandle不会执行。
二、非正常情况下的拦截器(第一个拦截器拦截,第二个拦截器拦截)
-
public class HandlerInterceptor1 implements HandlerInterceptor {
-
-
-
-
-
public boolean preHandle(HttpServletRequest request,
-
HttpServletResponse response, Object handler) throws Exception {
-
System.out.println("HandlerInterceptor1...preHandle");
-
-
-
-
-
public class HandlerInterceptor2 implements HandlerInterceptor {
-
-
-
-
-
public boolean preHandle(HttpServletRequest request,
-
HttpServletResponse response, Object handler) throws Exception {
-
System.out.println("HandlerInterceptor2...preHandle");
-
-
-
-
日志打印
HandlerInterceptor1...preHandle
总结
拦截器1 preHandle不放行,postHandle和afterCompletion不会执行。
拦截器1 preHandle不放行,拦截器2不执行。
SpringMVC自定义多个拦截器执行顺序
标签:title int net 日志信息 fill 接口 exce mode nbsp
原文地址:https://www.cnblogs.com/moxiaotao/p/9663351.html