SpringMVC框架

10.3 实验代码(多个拦截器)

  • 自定义拦截器类(两个)

package com.atguigu.springmvc.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 FirstHandlerInterceptor implements HandlerInterceptor {

 

@Override

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

System.out.println(this.getClass().getName() + ” – afterCompletion”);

}

 

@Override

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2, ModelAndView arg3) throws Exception {

System.out.println(this.getClass().getName() + ” – postHandle”);

}

 

@Override

public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2) throws Exception {

System.out.println(this.getClass().getName() + ” – preHandle”);

return true;

}

 

}

package com.atguigu.springmvc.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 SecondHandlerInterceptor implements HandlerInterceptor {

 

@Override

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

System.out.println(this.getClass().getName() + ” – afterCompletion”);

}

 

@Override

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2, ModelAndView arg3) throws Exception {

System.out.println(this.getClass().getName() + ” – postHandle”);

}

 

@Override

public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2) throws Exception {

System.out.println(this.getClass().getName() + ” – preHandle”);

return true;

}

 

}

  • 配置自定义拦截器

<mvc:interceptors>

<!– 声明自定义拦截器 –>

<bean id=”firstHandlerInterceptor”

  class=”com.atguigu.springmvc.interceptors.FirstHandlerInterceptor”></bean>

<!– 配置拦截器引用 –>

<mvc:interceptor>                        

<mvc:mapping path=”/empList”/>

<!– <mvc:exclude-mapping path=”/empList”/> –>

<bean id=”secondHandlerInterceptor”

         class=”com.atguigu.springmvc.interceptors.SecondHandlerInterceptor”></bean>

</mvc:interceptor>

</mvc:interceptors>

两个都是返回true :

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – preHandle

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – preHandle

************************************biz method*******************************

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – postHandle

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – postHandle

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – afterCompletion

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – afterCompletion

两个都是返回false:

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – preHandle

true,false

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – preHandle

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – preHandle

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – afterCompletion

false,true

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – preHandle

 

10.4 多个拦截方法的执行顺序

  • 关于执行顺序

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – preHandle

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – preHandle

************************************biz method*******************************

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – postHandle

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – postHandle

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – afterCompletion

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – afterCompletion

  • 执行顺序图解

 

  • 从源代码的执行角度分析流程:

boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {

if (getInterceptors() != null) {

for (int i = 0; i < getInterceptors().length; i++) {

HandlerInterceptor interceptor = getInterceptors()[i];

if (!interceptor.preHandle(request, response, this.handler)) {

triggerAfterCompletion(request, response, null);

return false;

}

this.interceptorIndex = i;

}

}

return true;

}

void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {

if (getInterceptors() == null) {

return;

}

for (int i = getInterceptors().length – 1; i >= 0; i–) {

HandlerInterceptor interceptor = getInterceptors()[i];

interceptor.postHandle(request, response, this.handler, mv);

}

}

void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)

throws Exception {

 

if (getInterceptors() == null) {

return;

}

for (int i = this.interceptorIndex; i >= 0; i–) {

HandlerInterceptor interceptor = getInterceptors()[i];

try {

interceptor.afterCompletion(request, response, this.handler, ex);

}

catch (Throwable ex2) {

logger.error(“HandlerInterceptor.afterCompletion threw exception”, ex2);

}

}

}

      源码分析:分析interceptorIndex的值情况

 


上一篇:
下一篇:
关于尚硅谷
教育理念
名师团队
学员心声
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
全国统一咨询电话:010-56253825
地址:北京市昌平区宏福科技园综合楼6层(北京校区)

深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

武汉市东湖高新开发区东湖网谷(武汉校区)