后期常识
- 拦截器的作用:拦截器的作用是在服务器解决申请资源之前、之后去做一些事件,比方在解决申请资源之前须要验证一下从前端传入过去的数据合不合规定,解决申请资源之后验证一下有没有产生异样
拦截器的三个办法:
preHandle:在解决申请资源之前做的办法
- 当返回值为true,示意放行,也就是能够解决申请资源
- 当返回值为false,示意不放行,也就是不能解决申请资源
- postHandle:在解决申请资源之后做的办法,如果preHandle不放行,那么这个办法也肯定不会执行到
- afterCompletion:是一个最终执行的办法,如果是只有一个拦截器可能看不到成果,然而当有多个拦截器时,会先执行多个拦截器的preHandle和postHandle,而后再执行afterCompletion
拦截器与filter的触发地位:
- filter:在咱们应用Tomcat的时候,它给咱们提供了filter过滤器,这个filter的触发地位是位于浏览器和服务器之间,也就是当申请的资源还没达到服务器时,filter就先对申请的资源做解决
- 拦截器:而拦截器是SpringMVC框架提供的,既然是框架提供的,那么当申请还没达到服务器时SpringMVC是没有方法解决的,也就是当SpringMVC接管到申请之后,它会先把申请资源交给拦截器,而后再放行给解决具体申请资源的Servlet类
拦截器与filter的拦挡范畴
- filter:什么都可能拦挡,既能拦挡Servlet资源,也能拦挡html,css,jsp等文件
- 拦截器:只能拦挡Servlet资源
一、拦截器的配置与应用
自定义拦截器类
public class MyInterceptor implements HandlerInterceptor { //事后解决,在资源解决前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("interceptor1:资源解决前"); return true;//放行 } //后置解决,在资源解决后 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("interceptor1:资源解决后"); } //最终实现后处理 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("interceptor1:最终办法"); } }
Controller执行类
@Controllerpublic class UserController { @RequestMapping("testIntercept.do") public String testIntercept(){ System.out.println("controller资源解决了"); return ""; }}
JSP
<%@ page contentType="text/html; charset=UTF-8" language="java" %><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>cai jin hong</title></head><body> <a href="testIntercept.do">拦截器</a></body></html>
Spring配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 手动配置这个类的驱动--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 配置拦截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*"/> <bean class="com.cjh.interceptor.MyInterceptor"></bean> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/*"/> <bean class="com.cjh.interceptor.MyInterceptor2"></bean> </mvc:interceptor> </mvc:interceptors></beans>
- 执行后果
二、拦截器的执行流程
1、一个拦截器
如下面代码所示,
- 拦截器会先执行preHandle办法,如果preHandle返回的后果为true,那么执行具体执行申请资源的Controller类
- 而后再执行postHandle办法
- 最初执行afterCompletion办法
2、多个拦截器
假如有两个拦截器,依据配置信息的配置地位,拦截器1的配置信息在拦截器2的后面,那么首先会执行拦截器1
1)拦截器1先执行preHandle办法
- 如果preHandle返回true,拦截器2执行preHandle办法,跳到第2步
- 如果preHandle返回false,拦截器2不执行preHandle办法,跳到第7步
2)当拦截器2的preHandle办法返回:
- true,执行controller的办法,跳到第3步
- false,跳到第6步
- 3)拦截器2执行postHandle办法
- 4)拦截器1执行postHandle办法
- 5)拦截器2执行afterCompletion办法
- 6)拦截器1执行afterCompletion办法
- 7)完结
- 留神:只有某个拦截器的preHandle办法放行了(返回true),那么不管它的postHandle办法能不能执行,它的afterCompletion办法肯定会执行,这就有点像try{}catch{}finally{}
- 多个拦截器的代码和单个的是一样的,只是配置的时候须要把每个拦截器都配置进去,每个拦截器是一个interceptor标签对象