SpringMVC 拦截器

1.自定义的拦截器必须 实现HandlerInterceptor 接口 :
     preHandle() :这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求 request 进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
      postHandle(): 这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用,在该方法中对用户请求request进行处理。
     afterCompletion(): 这个方法在 DispatcherServlet 完全处理完请求后被调用,可以在该方法中进行一些资源清理的操作。

2.实验代码(单个拦截器 ):
(1) 自定义拦截器类
package com.atguigu.springmvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class FirstInterceptor implements HandlerInterceptor {

     /**
     * preHandle():这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求 request 进行处理。
     * 如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;
     * 如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
     */

     @Override
     public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
               Object arg2) throws Exception {
          System.out.println(this.getClass().getName() + "- preHandle");
          return true;
     }



     // postHandle():这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用,在该方法中对用户请求request进行处理。

     @Override
     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
               Object arg2, ModelAndView arg3) throws Exception {
          System.out.println(this.getClass().getName() + "- postHandle");
     }

     //afterCompletion():这个方法在 DispatcherServlet 完全处理完请求后被调用,可以在该方法中进行一些资源清理的操作。

     @Override
     public void afterCompletion(HttpServletRequest arg0,
               HttpServletResponse arg1, Object arg2, Exception arg3)
               throws Exception {
          System.out.println(this.getClass().getName() + "- afterCompletion");
     }

}
(2) 配置拦截器
<mvc:interceptors>
<!-- 声明自定义拦截器,class为拦截器的地址,ID为拦截器的首字母小写 -->
<bean id="firstHandlerInterceptor"
      class="com.atguigu.springmvc.interceptors.FirstHandlerInterceptor">
</bean>
</mvc:interceptors>
(3) 流程图:

3.实验代码(多个拦截器):
(1) 自定义拦截器类(两个)
package com.atguigu.springmvc.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SecondInterceptor implements HandlerInterceptor {
     /**
     * afterCompletion():这个方法在 DispatcherServlet 完全处理完请求后被调用,可以在该方法中进行一些资源清理的操作。
     */
     @Override
     public void afterCompletion(HttpServletRequest arg0,
               HttpServletResponse arg1, Object arg2, Exception arg3)
               throws Exception {
          System.out.println(this.getClass().getName() + "= afterCompletion");
     }


     /**
     * postHandle():这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用,
     * 在该方法中对用户请求request进行处理。
     */
     @Override
     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
               Object arg2, ModelAndView arg3) throws Exception {
          System.out.println(this.getClass().getName() + "= postHandle");
     }

     /**
     * preHandle():这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求 request 进行处理。
     * 如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;
     * 如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
     */
     @Override
     public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
               Object arg2) throws Exception {
          System.out.println(this.getClass().getName() + "= preHandle");
          return true;
     }

}
(2) 配置自定义拦截器
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


     <!-- 设置扫描的包 -->
     <context:component-scan base-package="com.atguigu.springmvc"/>

     <!-- 视图解析器 -->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/views/"/>
           <property name="suffix" value=".jsp"></property>
     </bean>

     <!-- 处理静态资源
           default-servlet-name="default"   指定处理静态资源的Servlet是服务器Tomcat/conf/web.xml中提供的。
           Servlet的名称如果叫做"default",那么可以省略这个属性配置
      -->
     <mvc:default-servlet-handler />

     <!-- 配置 <mvc:default-servlet-handler/>解决静态资源访问问题,但是,原来的映射路径都找不到了-->
     <!-- <mvc:annotation-driven /> -->
     <mvc:annotation-driven/>

     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

     <mvc:interceptors>
           <bean class="com.atguigu.springmvc.interceptor.FirstInterceptor"></bean>
           <!-- <bean class="com.atguigu.springmvc.interceptor.SecondInterceptor"></bean> -->

           <mvc:interceptor>
                <mvc:mapping path="/testUpload"/>
                <mvc:exclude-mapping path="/testABC"/>
                <bean class="com.atguigu.springmvc.interceptor.SecondInterceptor"></bean>
           </mvc:interceptor>

     </mvc:interceptors>

</beans>
      说明:<bean>表示对所有的拦截, < mvc:interceptor>表示具有选择请的拦截,对那些拦截,对那些不拦截。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员学习圈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值