SpringMVC总结四:拦截器简单介绍
1) 正确的Handler对象
2) Handler的拦截器集合,这里的拦截器对象是:HandlerInterceptor
HandlerInterceptor接口中有三个方法:
preHandle:预处理回调方法,在Handler执行前执行,第三个参数为处理器(Controller 实现); 返回值:true 表示继续流程(如调用下一个拦截器或处理器); false 表示流程中断(如登录检查失败),不会继续调用其他的拦截器或处理器; postHandle :后处理回调方法,实现处理器的后处理(但在渲染视图之前) afterCompletion: :整个请求处理完毕回调方法,即在视图渲染完毕时回调
我们自定义拦截器的时候需要实现HandlerInterceptor接口。
拦截器的执行顺序:
异常中断流程:
简单实现案例:
自定义两个拦截器:
public class MyInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("*********拦截器1的前置处理方法"); return true;//这里如果return false 流程就中断不再继续往下执行controller了 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("*********拦截器1的后置处理方法"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("*********拦截器1的 完成后 处理方法"); } }
public class MyInterceptor2 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("*********拦截器2的前置处理方法"); return true;//这里如果return false 流程就中断不再继续往下执行controller了 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("*********拦截器2的后置处理方法"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("*********拦截器2的 完成后 处理方法"); } }
springmvc配置文件中配置自定义拦截器:
<?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"> <!--HandlerMapping和HandlerAdapter--> <mvc:annotation-driven/> <!--扫描controller--> <context:component-scan base-package="zy.com.controller"/> <!--视图解析器 viewResolver--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/views/"></property> <!--后缀--> <property name="suffix" value=".jsp"></property> </bean> <!--配置拦截器--> <mvc:interceptors> <!--方式一 拦截controller所有方法--> <bean class="zy.com.interceptor.MyInterceptor1"></bean> <!--方式二 配置拦截指定controller方法--> <mvc:interceptor> <mvc:mapping path="/test/jsonView.do"/> <bean class="zy.com.interceptor.MyInterceptor2"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
运行结果:
访问/test/jsonView.do(两个拦截器都起作用):
访问其他的controller方法(只有拦截器1起作用):