项目DEMO下载地址:twovv-test-spring-interceptor.rar
入门:
1、Spring拦截器的作用:
重点:拦截约等于临时阻断正常的方法执行流程。
1.1、概念:提供在不侵入原方法代码的情况下,通过拦截方法执行来达到执行其他方法或事情的目的(纯个人理解,非官方言论)。
1.2、Action方法执行前预处理,拦截并执行其他业务逻辑:
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object interceptor) throws Exception;
1.3、方法执行后生成视图之前(调用了Service方法并返回了ModelAndView,单页面还没有渲染这个时间),拦截并执行其他业务逻辑:
public void postHandle(HttpServletRequest request, HttpServletResponse response,Object interceptor, ModelAndView view) throws Exception;
1.4、方法执行后(此时页面已经渲染完毕了),拦截并执行其他业务逻辑:
public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object interceptor, Exception exception)throws Exception;
2、Spring拦截器的实现方式:
2.1、实现org.springframework.web.servlet.HandlerInterceptor接口。
2.2、继承org.springframework.web.servlet.handler.HandlerInterceptorAdapter抽象类。
2.3、实现org.springframework.web.context.request.WebRequestInterceptor接口。
2.4、继承org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter抽象类。
重要:HandlerInterceptor/HandlerInterceptorAdapter两者的主要区别是抽象类默认空实现了HandlerInterceptor接口,同时为preHandle()返回true,我一般直接实现HandlerInterceptor接口。
WebRequestInterceptor/HandlerInterceptor两者的主要区别是WebRequestInterceptor的preHandle无返回值,既无法终止拦截器链。
3、在Spring配置文件appInterceptor.xml中注册自定义的interceptor
<?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" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 测试拦截器 --><mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/test/interceptor.htm"/> <bean class="com.twovv.intercepter.MyImplementInterceptor"/> </mvc:interceptor></mvc:interceptors></beans>
3.1、Spring MVC并没有总的拦截器,不能对所有的请求进行前后拦截。Spring MVC的拦截器,是属于HandlerMapping级别的,可以有多个HandlerMapping,每个HandlerMapping可以有自己的拦截器。
当一个请求按Order值从小到大,顺序执行HandlerMapping接口的实现类时,哪一个先有返回,那就可以结束了,后面的HandlerMapping就不走了,本道工序就完成了。就转到下一道工序了。
拦截器执行时机:一个请求交给一个HandlerMapping时,这个HandlerMapping先找有没有处理器来处理这个请求,如何找到了,就执行拦截器,执行完拦截后,交给目标处理器。如果没有找到处理器,那么这个拦截器就不会被执行。
<mvc:interceptors> <bean class="com.twovv.GlobalInteceptor" /> </mvc:interceptors>
直接将bean配置到mvc:interceptors,将会应用到所有的HandlerMapping,达到伪全局拦截器的目的。
3.2、匹配URI拦截具体的HandleMapping
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/test/interceptor.htm"/> <bean class="com.twovv.intercepter.MyImplementInterceptor"/> </mvc:interceptor></mvc:interceptors>
mvc:mapping path匹配规则详细记录:
3.2.1、path必须以/开头进行匹配,因为默认的是匹配URI。
3.2.2、path匹配目录使用**,如/**/代表匹配所有的一级目录。
3.2.3、path匹配文件使用*,如/**/*.htm代表匹配所有一级目录下的所有htm文件。
3.2.4、path可以匹配以指定文字开头或者结尾或者中间包含的URI,如:
/twov**/twov*.htm/*wov*/two*.htm/*wovv/*wovv.htm
3.3、直接将interceptor注册到HandlerMapping(我个人不推荐)
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="com.twovv.GlobalInteceptor"></bean> </list> </property> </bean>
这样就必须手动注册HandlerMapping,不能再使用<mvc:annotation-driven />方式,
而且这种配置不够灵活,还是推荐前两种方式。
联系客服