1.攔截器在一次請求中的執行流程
2.攔截器入門案例
2.1springMVC.xml的編寫
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.atguigu"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 靜態資源需要默認的servlet來處理 --> <mvc:default-servlet-handler/> <mvc:annotation-driven></mvc:annotation-driven> <mvc:interceptors> <!-- 默認攔截所有請求--> <bean class="com.atguigu.interceptor.FirstInterceptor"></bean> <bean class="com.atguigu.interceptor.SecondInterceptor"></bean>
<!-- 此方式要求攔截器上必須加上@Component --> <!-- <ref bean="firstInterceptor"/> --> <!-- 自定義攔截方式--> <!-- <mvc:interceptor> <bean></bean> <mvc:mapping path=""/>用於登入驗證,如果session中沒有值,攔截。有值,不攔截 <mvc:exclude-mapping path=""/>有些功能不需要攔截,例如跳轉到登入頁面 </mvc:interceptor> --> </mvc:interceptors> </beans>
2.2 攔截器的編寫
FirstInterceptor
package com.atguigu.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @Component public class FirstInterceptor implements HandlerInterceptor { public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub System.out.println("First:afterCompletion"); } public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub System.out.println("First:postHandle"); } public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { // TODO Auto-generated method stub System.out.println("First:preHandle"); return true; } }
SecondInterceptor
package com.atguigu.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @Component public class SecondInterceptor implements HandlerInterceptor { public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub System.out.println(" Second:afterCompletion"); } public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub System.out.println(" Second:postHandle"); } public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { // TODO Auto-generated method stub System.out.println(" Second:preHandle"); return true; } }
2.3攔截器的執行順序
First:preHandle
Second:preHandle
Second:postHandle
First:postHandle
Second:afterCompletion
First:afterCompletion
/** * 當有多個攔截器時, * preHandle,按照攔截器數組的正向順序執行 * postHandle,按照攔截器數組的反向順序執行 * afterCompletion,按照攔截器數組的反向順序執行 * * 當多個攔截器的preHandle有不同的值時, * 第一個返回false,第二個返回false,只有第一個攔截器的preHandle()會執行 * 第一個返回true,第二個返回false,(全部)第一、二個攔截器的preHandle()、(全部)postHandle()都不會執行, * 而afterCompletion()有(返回false的攔截器之前的)第一個會執行 * 第一個返回false,第二個返回true:只有第一個的perhandle()會執行 * * @return */