SpringMVC中拦截器的配置和使用
一.配置单个拦截器
先定义一个类,让他实现一个接口 HandlerInterceptor
package demo20interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by mycom on 2018/4/2. */ public class MyInteceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("preHandle======"); return true; } public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle======="); } public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion========"); } }
定义一个控制器类
package demo20interceptor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by mycom on 2018/4/2. */ @Controller public class FirstController { @RequestMapping("/first") public String doFirst(){ System.out.println("Handle Model"); return "success"; } }
配置文件中
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="demo20interceptor"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/fileupload/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean> <!--拦截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**/"/> <bean class="demo20interceptor.MyInteceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!--注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
测试的时候直接在地址栏中输入first就可以了
运行的结果是:
preHandle======
Handle Model
postHandle=======
afterCompletion========
二.配置多个拦截器
配置多个拦截器,就要在多定义一个拦截器
package demo20interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by mycom on 2018/4/2. */ public class MyInterceptor2 implements HandlerInterceptor { public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("second preHandle"); return true; } public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("second postHandle"); } public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("second afterCompletion"); } }
在配置文件中,也同样要多配置一个节点