方法名 | 返回值 | 说明 |
---|---|---|
preHandle () | boolean |
该方法在控制器方法之前执行,其返回值用来表示是否中断后续操作。
|
postHandle () | void | 该方法会在控制器方法调用之后,解析式图之前执行。我们可以通过此方法对请求域中的模型(Model)数据和视图做出进一步的修改。 |
afterCompletion () | void | 该方法会在整个请求完成后,即视图渲染结束之后执行。我们可以通过该方法实现资源清理、日志记录等工作。 |
package net.biancheng.c.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor 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 执行"); } }
标签 | 一级子标签 | 二级子标签 | 说明 |
---|---|---|---|
<mvc:interceptors> | <bean> | - | <mvc:interceptors> 标签的子标签,用于定义一个全局拦截器,对所有的请求进行拦截。 |
<ref> | - |
<mvc:interceptors> 标签的子标签,用于定义一个全局拦截器的引用,对所有的请求进行拦截。 <mvc:interceptors> 标签的 <ref> 子标签不能单独使用,它需要与 <bean> 标签(<mvc:interceptors> 标签内或 <mvc:interceptors>标签外)或 @Component 等注解配合使用,以保证<ref> 标签配置的拦截器是 Spring IOC 容器中的一个组件。 |
|
<mvc:interceptor> | <mvc:mapping> |
<mvc:interceptor> 标签用于定义一个指定拦截路径的拦截器。 <mvc:mapping> 标签则是<mvc:interceptor> 的子标签,用来配置拦截器拦截的路径,该路径则是通过其属性“path”中定义的。例如,path 的属性值为“/**”时,表示拦截所有请求;而“/hello”则表示拦截路径为“/hello”的请求。 |
|
<mvc:exclude-mapping> | <mvc:exclude-mapping> 为<mvc:interceptor> 的子标签,用来配置不需要被拦截器拦截的路径。 | ||
<bean> | <bean> 为<mvc:interceptor> 的子标签,用来定义一个指定了拦截路径的拦截器。 |
<!--配置拦截器--> <mvc:interceptors> <bean class="net.biancheng.c.interceptor.MyInterceptor"></bean> </mvc:interceptors>
<!--将自定义的拦截器放到 ioc 容器中--> <bean id="interceptor" class="net.biancheng.c.interceptor.MyInterceptor"></bean> <!--配置拦截器--> <mvc:interceptors> <!--通过 ref 配置全局拦截器--> <ref bean="interceptor"></ref> </mvc:interceptors>
<!--配置拦截器--> <mvc:interceptors> <!--拦截器 1--> <mvc:interceptor> <!--配置拦截器拦截的请求路径--> <mvc:mapping path="/**"/> <!--配置拦截器不需要拦截的请求路径--> <mvc:exclude-mapping path="/login"/> <mvc:exclude-mapping path="/"/> <!--定义在 <mvc:interceptors> 下,表示拦截器只对指定路径的请求进行拦截--> <bean class="net.biancheng.c.interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
<mvc:mapping>
→ <mvc:exclude-mapping>
→ <bean>
的顺序,否则就会报错。其次,以上这三种配置拦截器的方式,我们可以根据自身的需求以任意的组合方式进行配置,以实现在 <mvc:interceptors> 标签中定义多个拦截器的目的。
图1:单个拦截器处理流程
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--请求和响应的字符串过滤器--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--来处理 PUT 和 DELETE 请求的过滤器--> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置 DispatcherServlet 的一个初始化参数:spring mvc 配置文件按的位置和名称--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <!--作为框架的核心组件,在启动过程中有大量的初始化操作要做 而这些操作放在第一次请求时才执行会严重影响访问速度 因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <!--设置springMVC的核心控制器所能处理的请求的请求路径 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径 但是/不能匹配.jsp请求路径的请求--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启组件扫描--> <context:component-scan base-package="net.biancheng.c"></context:component-scan> <!-- 配置 Thymeleaf 视图解析器 --> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean> <!-- view-name:设置请求地址所对应的视图名称--> <mvc:view-controller path="/hello" view-name="success"></mvc:view-controller> <!-- 当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签--> <mvc:annotation-driven></mvc:annotation-driven> <!-- <!–静态资源映射–>--> <mvc:default-servlet-handler/> <!--配置拦截器--> <mvc:interceptors> <!--拦截器 1--> <mvc:interceptor> <!--配置拦截器拦截的请求路径--> <mvc:mapping path="/**"/> <!--配置拦截器不需要拦截的请求路径--> <mvc:exclude-mapping path="/login"/> <mvc:exclude-mapping path="/"/> <!--定义在 <mvc:interceptors> 下,表示拦截器只对指定路径的请求进行拦截--> <bean id="interceptor" class="net.biancheng.c.interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
package net.biancheng.c.interceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor----> preHandleO() 执行"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor----> postHandle() 执行"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor----> afterCompletion() 执行"); } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>访问成功!</h1> </body> </html>
图2:访问成功
MyInterceptor----> preHandleO() 执行 MyInterceptor----> postHandle() 执行 MyInterceptor----> afterCompletion() 执行
图3:多个拦截器的执行流程
package net.biancheng.c.interceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class MyInterceptor2 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor2----> preHandleO() 执行"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor2----> postHandle() 执行"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor2---> afterCompletion() 执行"); } }
package net.biancheng.c.interceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class MyInterceptor3 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor3----> preHandleO() 执行"); // 返回值设置为 false return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor3----> postHandle() 执行"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor3---> afterCompletion() 执行"); } }
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启组件扫描--> <context:component-scan base-package="net.biancheng.c"></context:component-scan> <!-- 配置 Thymeleaf 视图解析器 --> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean> <!-- <!– view-name:设置请求地址所对应的视图名称–>--> <mvc:view-controller path="/hello" view-name="success"></mvc:view-controller> <!-- 当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签--> <mvc:annotation-driven></mvc:annotation-driven> <!-- <!–静态资源映射–>--> <mvc:default-servlet-handler/> <!--配置拦截器--> <mvc:interceptors> <!--拦截器 1--> <mvc:interceptor> <!--配置拦截器拦截的请求路径--> <mvc:mapping path="/**"/> <!--配置拦截器不需要拦截的请求路径--> <mvc:exclude-mapping path="/login"/> <mvc:exclude-mapping path="/"/> <!--定义在 <mvc:interceptors> 下,表示拦截器只对指定路径的请求进行拦截--> <bean class="net.biancheng.c.interceptor.MyInterceptor"></bean> </mvc:interceptor> <!--拦截器 MyInterceptor2--> <mvc:interceptor> <!--配置拦截器拦截的请求路径--> <mvc:mapping path="/**"/> <!--配置拦截器不需要拦截的请求路径--> <mvc:exclude-mapping path="/login"/> <mvc:exclude-mapping path="/"/> <!--定义在 <mvc:interceptors> 下,表示拦截器只对指定路径的请求进行拦截--> <bean class="net.biancheng.c.interceptor.MyInterceptor2"></bean> </mvc:interceptor> <!--拦截器 MyInterceptor3--> <mvc:interceptor> <!--配置拦截器拦截的请求路径--> <mvc:mapping path="/**"/> <!--配置拦截器不需要拦截的请求路径--> <mvc:exclude-mapping path="/login"/> <mvc:exclude-mapping path="/"/> <!--定义在 <mvc:interceptors> 下,表示拦截器只对指定路径的请求进行拦截--> <bean class="net.biancheng.c.interceptor.MyInterceptor3"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
MyInterceptor----> preHandleO() 执行 MyInterceptor2----> preHandleO() 执行 MyInterceptor3----> preHandleO() 执行 MyInterceptor2----> afterCompletion() 执行 MyInterceptor----> afterCompletion() 执行
本文链接:http://task.lmcjl.com/news/15626.html