这篇文章主要介绍“springMVC如何自定义拦截器”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springMVC如何自定义拦截器”文章能帮助大家解决问题。
springMVC也可以使用拦截器对请求拦截处理,用户可以自定义拦截器来实现特定的功能,自定义拦截器必须实现HanderInterceptor接口;
HanderInterceptor接口方法有:preHandle、postHandle、afterCompletion;
这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求request进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false;
这个方法在业务处理器处理完请求后,但是DispatcherServlet向客户端返回响应前被调用;在该方法中对用户请求request进行处理;
这个方法在DispatcherServlet完全处理完请求后被调用;可以在该方法中进行一些资源清理的操作;
web.xml配置:只配置了DispatcherServlet
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 <servlet> 7 <servlet-name>springDispatcherServlet</servlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 9 <!-- 配置DispatcherServletd 一个初始化参数:配置springmvc配置文件的位置和名称 --> 10 <!-- 实际上也可以不通过 contextConfigLocation 来配置Springmvc的配置文件,而是用默认的 即默认的配置文件为 11 /WEB-INF/<servlet-name>-servlet.xml 本项目默认位置配置文件即为: /WEB-INF/springDispatcherServlet-servlet.xml --> 12 <init-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:spring.xml</param-value> 15 </init-param> 16 <!-- 表示springDispatcherServlet在加载的时候被创建 --> 17 <load-on-startup>1</load-on-startup> 18 </servlet> 19 20 <!-- Map all requests to the DispatcherServlet for handling --> 21 <servlet-mapping> 22 <servlet-name>springDispatcherServlet</servlet-name> 23 <url-pattern>/</url-pattern> 24 </servlet-mapping> 25 </web-app>
spring.xml配置:基础配置+拦截器配置;
基础配置包括:包扫描的配置、视图解析器配置、<mvc:annotation-driven></mvc:annotation-driven>;
拦截器配置:
<mvc:interceptors> <!-- 配置自定义拦截器 --> <bean class="interceptors.FirstInterceptor"></bean> </mvc:interceptors>
完整文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="handler"></context:component-scan> 11 <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> 12 <bean 13 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/WEB-INF/views/"></property> 15 <property name="suffix" value=".jsp"></property> 16 </bean> 17 18 <mvc:annotation-driven></mvc:annotation-driven> 19 20 <mvc:interceptors> 21 <!-- 配置自定义拦截器 --> 22 <bean class="interceptors.FirstInterceptor"></bean> 23 </mvc:interceptors> 24 25 </beans>
必须实现HanderInterceptor接口
1 package interceptors; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.HandlerInterceptor; 7 import org.springframework.web.servlet.ModelAndView; 8 9 /** 10 * 拦截器<br> 11 * 多个拦截器的情况:preHandle 顺序执行;postHandle倒叙执行;afterCompletion倒叙执行 12 * 13 * @author lixiuming 14 * 15 */ 16 public class FirstInterceptor implements HandlerInterceptor { 17 /** 18 * 该方法在目标方法之前被调用<br> 19 * 若返回值为true,则继续调用后续的拦截器和目标方法<br> 20 * 若返回值为false,则不会再执行后续的拦截器和目标方法;<br> 21 * 使用场景可以考虑做权限,日志、事务等 22 */ 23 @Override 24 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 25 throws Exception { 26 System.out.println("preHandle"); 27 // TODO Auto-generated method stub 28 return HandlerInterceptor.super.preHandle(request, response, handler); 29 } 30 31 /** 32 * 调用目标方法之后,但渲染视图之前被调用;使用场景 可以对请求域中的属性或者视图做出修改 33 */ 34 @Override 35 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 36 ModelAndView modelAndView) throws Exception { 37 System.out.println("postHandle"); 38 // TODO Auto-generated method stub 39 HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); 40 } 41 42 /** 43 * 渲染视图之后被调用; 使用场景:释放资源 44 */ 45 @Override 46 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 47 throws Exception { 48 System.out.println("afterCompletion"); 49 // TODO Auto-generated method stub 50 HandlerInterceptor.super.afterCompletion(request, response, handler, ex); 51 } 52 53
1 package handler; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @RequestMapping("/emp") 7 @Controller 8 public class EmployeeController { 9 10 @RequestMapping("/testInteceptors") 11 public String testFileUpload() { 12 System.out.println("testInteceptors"); 13 return "success"; 14 } 15
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 success 11 </body> 12 </html>
访问http://localhost:8080/DataOperate/emp/testInteceptors时,页面跳转到success页面;且控制台台打印了:
preHandle
testInteceptors
postHandle
afterCompletion
这里的路径是访问路径是/emp/testIndex,也就是说,访问/emp/testIndex时,调用第二个拦截器;
1 <mvc:interceptors> 2 <!-- 配置自定义拦截器 --> 3 <bean class="interceptors.FirstInterceptor"></bean> 4 <!-- 配置拦截器(不)作用的路径 --> 5 <mvc:interceptor> 6 <mvc:mapping path="/emp/testIndex"/> 7 <bean class="interceptors.SecondInterceptor"></bean> 8 </mvc:interceptor> 9 </mvc:interceptors>
1 package interceptors; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.HandlerInterceptor; 7 import org.springframework.web.servlet.ModelAndView; 8 9 /** 10 * 拦截器 11 * 12 * @author lixiuming 13 * 14 */ 15 public class SecondInterceptor implements HandlerInterceptor { 16 /** 17 * 该方法在目标方法之前被调用<br> 18 * 若返回值为true,则继续调用后续的拦截器和目标方法<br> 19 * 若返回值为false,则不会再执行后续的拦截器和目标方法;<br> 20 * 使用场景可以考虑做权限,日志、事务等 21 */ 22 @Override 23 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 24 throws Exception { 25 System.out.println("SecondInterceptor preHandle"); 26 // TODO Auto-generated method stub 27 return HandlerInterceptor.super.preHandle(request, response, handler); 28 } 29 30 /** 31 * 调用目标方法之后,但渲染视图之前被调用;使用场景 可以对请求域中的属性或者视图做出修改 32 */ 33 @Override 34 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 35 ModelAndView modelAndView) throws Exception { 36 System.out.println(" SecondInterceptor postHandle"); 37 // TODO Auto-generated method stub 38 HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); 39 } 40 41 /** 42 * 渲染视图之后被调用; 使用场景:释放资源 43 */ 44 @Override 45 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 46 throws Exception { 47 System.out.println(" SecondInterceptor afterCompletion"); 48 // TODO Auto-generated method stub 49 HandlerInterceptor.super.afterCompletion(request, response, handler, ex); 50 } 51 52
1 package handler; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @RequestMapping("/emp") 7 @Controller 8 public class EmployeeController { 9 10 @RequestMapping("/testInteceptors") 11 public String testFileUpload() { 12 System.out.println("testInteceptors"); 13 return "success"; 14 } 15 16 @RequestMapping("/testIndex") 17 public String testIndex() { 18 System.out.println("testInteceptors"); 19 return "forward:/index.jsp"; 20 } 21
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@page import="java.util.*"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <h4>welcom to lean interceptor !</h4> 12 13 14 </body> 15 </html>
访问:http://localhost:8080/DataOperate/emp/testInteceptors时,页面跳转到success页面;且控制台台打印了:
preHandle
testInteceptors
postHandle
afterCompletion
访问:http://localhost:8080/DataOperate/emp/testIndex时,页面跳转到index.jsp页面;且控制台台打印了:
preHandle
SecondInterceptor preHandle
testInteceptors
SecondInterceptor postHandle
postHandle
SecondInterceptor afterCompletion
afterCompletion
多个拦截器运行流程:多个拦截器的情况:preHandle 顺序执行;postHandle倒叙执行;afterCompletion倒叙执行
关于“springMVC如何自定义拦截器”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。