今天就是把有關(guān)攔截器的知識做一個(gè)總結(jié)。
1.攔截器概述
1.1 什么是攔截器?
Spring MVC中的攔截器(Interceptor)類似于Servlet中的過濾器(Filter),它主要用于攔截用戶請求并作相應(yīng)的處理。例如通過攔截器可以進(jìn)行權(quán)限驗(yàn)證、記錄請求信息的日志、判斷用戶是否登錄等。
要使用Spring MVC中的攔截器,就需要對攔截器類進(jìn)行定義和配置。通常攔截器類可以通過兩種方式來定義。
1.通過實(shí)現(xiàn)HandlerInterceptor接口,或繼承HandlerInterceptor接口的實(shí)現(xiàn)類(如HandlerInterceptorAdapter)來定義。
2.通過實(shí)現(xiàn)WebRequestInterceptor接口,或繼承WebRequestInterceptor接口的實(shí)現(xiàn)類來定義。
以實(shí)現(xiàn)HandlerInterceptor接口方式為例,自定義攔截器類的代碼如下:
public class CustomInterceptor implements HandlerInterceptor{
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)throws Exception {
return false;
}
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler,
Exception ex) throws Exception {
}
}
上述代碼中,自定義攔截器實(shí)現(xiàn)了HandlerInterceptor接口,并實(shí)現(xiàn)了接口中的三個(gè)方法:
preHandle() 方法:該方法會(huì)在控制器方法前執(zhí)行,其返回值表示是否中斷后續(xù)操作。當(dāng)其返回值為true時(shí),表示繼續(xù)向下執(zhí)行;
當(dāng)其返回值為false時(shí),會(huì)中斷后續(xù)的所有操作(包括調(diào)用下一個(gè)攔截器和控制器類中的方法執(zhí)行等)。 postHandle()方法:該方法會(huì)在控制器方法調(diào)用之后,且解析視圖之前執(zhí)行。可以通過此方法對請求域中的模型和視圖做出進(jìn)一步的修改。 afterCompletion()方法:該方法會(huì)在整個(gè)請求完成,即視圖渲染結(jié)束之后執(zhí)行??梢酝ㄟ^此方法實(shí)現(xiàn)一些資源清理、記錄日志信息等工作。
1.2 攔截器的配置
開發(fā)攔截器就像開發(fā)servlet或者filter一樣,都需要在配置文件進(jìn)行配置,配置代碼如下:
<!--配置攔截器-->
<mvc:interceptors>
<!--<bean class="com.ma.interceptor.CustomeInterceptor" />-->
<!--攔截器1-->
<mvc:interceptor>
<!--配置攔截器的作用路徑-->
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path=""/>
<!--定義在<mvc:interceptor>下面的表示匹配指定路徑的請求才進(jìn)行攔截-->
<bean class="com.ma.interceptor.Intercptor1"/>
</mvc:interceptor>
<!--攔截器2-->
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean class="com.ma.interceptor.Interceptor2"/>
</mvc:interceptor>
上面的代碼中,<mvc:interceptors>元素用于配置一組攔截器,基子元素<bean>中定義的是全局?jǐn)r截器,它會(huì)攔截所有的請求;而<mvc:interceptor>元素中定義的是指定路徑的攔截器,它會(huì)對指定路徑下的請求生效。<mvc:interceptor>元素的子元素<mvc:mapping>用于配置攔截器作用的路徑,該路徑在其屬性path 中定義。如上述代碼中 path 的屬性值“/**” 表示攔截所有路徑,“/hello” 表示攔截所有以 “/hello” 結(jié)尾的路徑。如果在請求路徑中包含不需要攔截的內(nèi)容,還可以通過<mvc:exclude-mapping>元素進(jìn)行配置。
注意:<mvc:interceptor>中的子元素必須按照上述代碼中的配置順序進(jìn)行編寫,即<mvc:mapping> <mvc:exclude-mapping> <bean>,否則文件會(huì)報(bào)錯(cuò)。
2. 攔截器的執(zhí)行流程
2.1 單個(gè)攔截器的執(zhí)行流程
在運(yùn)行程序時(shí),攔截器的執(zhí)行是有一定順序的,該順序與配置文件中所定義的攔截器的順序相關(guān)。
單個(gè)攔截器,在程序中的執(zhí)行流程如下圖所示:

1.程序先執(zhí)行preHandle()方法,如果該方法的返回值為true,則程序會(huì)繼續(xù)向下執(zhí)行處理器中的方法,否則將不再向下執(zhí)行。
2.在業(yè)務(wù)處理器(即控制器Controller類)處理完請求后,會(huì)執(zhí)行postHandle()方法,然后會(huì)通過DispatcherServlet向客戶端返回響應(yīng)。
3.在DispatcherServlet處理完請求后,才會(huì)執(zhí)行afterCompletion()方法。
測試案例:
通過一個(gè)測試程序來驗(yàn)證它的執(zhí)行流程。
新建一個(gè)web項(xiàng)目,準(zhǔn)備好SpringMVC程序運(yùn)行所需要的JAR包,在web.xml中配置前端過慮器和初始化加載信息。
新建一個(gè)測試controller,代碼如下:
/**
* @author mz
* @version V1.0
* @Description: 攔截器測試
*/
@Controller
public class HelloController {
@RequestMapping("/hello")
public String Hello() {
System.out.println("Hello!");
return "success";
}
}
然后,新建一個(gè)攔截器,實(shí)現(xiàn)HandlerInterceptor接口,并實(shí)現(xiàn)其中的方法。
/**
* @author mz
* @version V1.0
* @Description: 實(shí)現(xiàn)了HandlerInterceptor接口的自定義攔截器類
*/
public class CustomeInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o)
throws Exception {
System.out.println("CustomInterceptor....preHandle");
//對瀏覽器的請求進(jìn)行放行處理
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView)
throws Exception {
System.out.println("CustomInterceptor....postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e)
throws Exception {
System.out.println("CustomInterceptor....afterCompletion");
}
}
在配置文件中配置攔截器。
<beans xmlns="http://www./schema/beans"
xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:mvc="http://www./schema/mvc"
xmlns:context="http://www./schema/context"
xsi:schemaLocation="http://www./schema/beans
http://www./schema/beans/spring-beans-4.3.xsd
http://www./schema/mvc
http://www./schema/mvc/spring-mvc-4.3.xsd
http://www./schema/context
http://www./schema/context/spring-context-4.3.xsd">
<!--定義組件掃描器,指定需要掃描的包-->
<context:component-scan base-package="com.ma.controller"/>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置攔截器-->
<mvc:interceptors>
<bean class="com.ma.interceptor.CustomeInterceptor" />
</beans>
把項(xiàng)目發(fā)布到tomcat中,運(yùn)行測試:

2.2 多個(gè)攔截器的執(zhí)行流程
多個(gè)攔截器(假設(shè)有兩個(gè)攔截器Interceptor1和Interceptor2,并且在配置文件中, Interceptor1攔截器配置在前),在程序中的執(zhí)行流程如下圖所示:

從圖可以看出,當(dāng)有多個(gè)攔截器同時(shí)工作時(shí),它們的preHandle()方法會(huì)按照配置文件中攔截器的配置順序執(zhí)行,而它們的postHandle()方法和afterCompletion()方法則會(huì)按照配置順序的反序執(zhí)行。
測試案例:
新建兩個(gè)攔截器:
/**
* @author mz
* @version V1.0
* @Description: 第一個(gè)攔截器
*/
public class Intercptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("Interceptor1....preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("Interceptor1....postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("Interceptor1....afterCompletion");
}
/**
* @author mz
* @version V1.0
* @Description: 第二個(gè)攔截器
*/
public class Interceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("Interceptor2....preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("Interceptor2....postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("Interceptor2....afterCompletion");
}
}
添加配置信息:
<!--攔截器1-->
<mvc:interceptor>
<!--配置攔截器的作用路徑-->
<mvc:mapping path="/**"/>
<!--定義在<mvc:interceptor>下面的表示匹配指定路徑的請求才進(jìn)行攔截-->
<bean class="com.ma.interceptor.Intercptor1"/>
</mvc:interceptor>
<!--攔截器2-->
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean class="com.ma.interceptor.Interceptor2"/>
</mvc:interceptor>
測試運(yùn)行:

從結(jié)果可以看出,執(zhí)行的順序和圖片中是一樣的。
如果第一個(gè)攔截器return true; 而第二個(gè)攔截器 return false;結(jié)果如下:

小結(jié)
首先學(xué)習(xí)如何在Spring MVC項(xiàng)目中定義和配置攔截器,然后了解單個(gè)攔截器和多個(gè)攔截器的執(zhí)行流程。
|