Spring Web MVC的处理器拦截器 双向 request response
类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。
拦截器拦截的请求是建立在前端控制器配置之下的,若DispatcherServlet拦截的是*.action,则拦截器即使配置 /**,则拦截器拦截的也只是所有 *.action的请求。若DispatcherServlet拦截的是 /,则拦截器配置 /**才是拦截所有资源。
preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Controller实现);
返回值:true表示继续流程(如调用下一个拦截器或处理器);
false表示流程中断(如登录检查失败),不会继续调用其他的拦截器或处理器,此时我们需要通过response来产生响应;
postHandle:后处理回调方法,实现处理器的后处理(但在渲染视图之前),此时我们可以通过modelAndView(模型和视图对象)对模型数据进行处理或对视图进行处理,modelAndView也可能为null。
afterCompletion:整个请求处理完毕回调方法,即在视图渲染完毕时回调,如性能监控中我们可以在此记录结束时间并输出消耗时间,还可以进行一些资源清理,类似于try-catch-finally中的finally,但仅调用处理器执行链中preHandle返回true的拦截器的afterCompletion。
下边是springMVC的拦截器实现
一.定义拦截器 (单个拦截器) 实现HandlerInterceptor
拦截器 */ public class Intecptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("preHandle====");
//这里如果返回的是false 则不执行拦截器其他的方法 如果是true 则进入另外两个方法 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===="); } }
二.xml配置 记得加注解驱动
<!--包扫描器--> <context:component-scan base-package="day17"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--单个拦截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class=" day17.Intecptor"/> </mvc:interceptor> </mvc:interceptors>
定义多个拦截器
拦截器1
/* 拦截器 */ public class Intecptor 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===="); } }
拦截器2
public class Intecptors 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===="); } }
xml配置
<!--包扫描器--> <context:component-scan base-package="day17"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--多个拦截器--> <!-- <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class=" day17.Intecptor"/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class=" day17.Intecptors"/> </mvc:interceptor> </mvc:interceptors>--> <!--注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans>