拦截器Interceptor

来源:互联网 发布:企业网络拓扑图配置 编辑:程序博客网 时间:2024/06/11 20:37


SpringMVC 中的Interceptor拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那样子判断当前时间是否是购票时间.

Interceptor定义

1.       Interceptor类要直接或者间接实现了Spring 的HandlerInterceptor接口,抽象类HandlerInterceptorAdapter ;

2.       实现Spring的WebRequestInterceptor接口,或者是继承实现了WebRequestInterceptor的类。

(一)实现HandlerInterceptor接口,覆写三个方法,我们就是通过这三个方法来对用户的请求进行拦截处理的。

   (1 )preHandle (HttpServletRequest request,HttpServletResponse response, Object handle) 方法,该方法将在请求处理之前进行调用。SpringMVC 中的Interceptor 是链式的调用的,每个Interceptor 的调用会依据它的声明顺序依次执行。该方法的返回值是布尔值Boolean 类型的,当它返回为false 时,表示请求结束,后续的Interceptor 和Controller都不会再执行;当返回值为true 时就会继续调用下一个Interceptor 的preHandle 方法,如果已经是最后一个Interceptor 的时候就会是调用当前请求的Controller 方法。

   (2 )postHandle (HttpServletRequest request,HttpServletResponse response, Object handle, ModelAndView modelAndView) 方法,由preHandle 方法的解释我们知道这个方法包括后面要说到的afterCompletion 方法都只能是在当前所属的Interceptor 的preHandle 方法的返回值为true 时才能被调用。Controller 方法调用之后执行,但是它会在DispatcherServlet 进行视图返回渲染之前被调用,所以我们可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。相反的,也就是说先声明的Interceptor 的postHandle方法反而会后执行。

   (3 )afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handle, Exception ex) 方法,该方法也是需要当前对应的Interceptor 的preHandle方法的返回值为true 时才会执行。顾名思义,该方法将在整个请求结束之后,也就是在DispatcherServlet 渲染了对应的视图之后执行。这个方法的主要作用是用于进行资源清理工作的。

(二)实现WebRequestInterceptor 接口

  三个方法都传递了同一个参数WebRequest ,那么这个WebRequest 是什么呢?这个WebRequest 是Spring 定义的一个接口,它里面的方法定义都基本跟HttpServletRequest 一样,在WebRequestInterceptor 中对WebRequest 进行的所有操作都将同步到HttpServletRequest 中,然后在当前请求中一直传递。

   (1 )preHandle(WebRequest request) 方法。这个方法跟HandlerInterceptor 中的preHandle是不同的,主要区别在于该方法的返回值是void ,也就是没有返回值,所以我们一般主要用它来进行资源的准备工作,比如我们在使用Hibernate 的时候可以在这个方法中准备一个Hibernate 的Session 对象,然后利用WebRequest 的setAttribute(name, value, scope) 把它放到WebRequest 的属性中。

   (2 )postHandle(WebRequest request, ModelMap model) 方法。在Controller 方法调用之后被调用,但是会在视图返回被渲染之前被调用,所以可以在这个方法里面通过改变数据模型ModelMap 来改变数据的展示。

   (3 )afterCompletion(WebRequest request, Exception ex)方法。

 

把定义的拦截器类加到SpringMVC的拦截体系中

 

         1.在SpringMVC的配置文件中加上支持MVC的schema

Xml代码  

<beansxmlns="http://www.springframework.org/schema/beans" 

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x mlns: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-3.0.xsd 

    http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/context/spring-context-3.0.xsd 

    http://www.springframework.org/schema/mvc 

    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

        

 

        2.使用mvc:interceptors标签来声明需要加入到SpringMVC拦截器链中的拦截器

Xml代码

<mvc:interceptors> 

     <beanclass="com.host.app.web.interceptor.AllInterceptor"/> 

      <mvc:interceptor> 

      <mvc:mappingpath="/test/number.do"/> 

      <beanclass="com.host.app.web.interceptor.LoginInterceptor"/> 

      </mvc:interceptor> 

</mvc:interceptors> 

        

    在mvc:interceptors标签下声明interceptor主要有两种方式:

       (1)直接定义一个Interceptor实现类的bean对象。使用这种方式声明的Interceptor拦截器将会对所有的请求进行拦截。

        (2)使用mvc:interceptor标签进行声明。使用这种方式进行声明的Interceptor可以通过mvc:mapping子标签来定义需要进行拦截的请求路径。
0 0