1.springMVC中拦截器实现这个接口implements HandlerInterceptor
第一个拦截器:
- public class MyInterceptor1 implements HandlerInterceptor{
- public void afterCompletion(HttpServletRequest arg0,
- HttpServletResponse arg1, Object arg2, Exception arg3)
- throws Exception {
- System.out.println("=======> my interceptor1 :afterCompletion() ");
- }
- public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
- Object arg2, ModelAndView arg3) throws Exception {
- // TODO Auto-generated method stub
- System.out.println("=======> my interceptor1:postHandle() ");
- }
- public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
- Object arg2) throws Exception {
- System.out.println("=======> my interceptor1:preHandle() ");
- return true;
- }
- }
第二个拦截器:
- public class MyInterceptor2 implements HandlerInterceptor{
- public void afterCompletion(HttpServletRequest arg0,
- HttpServletResponse arg1, Object arg2, Exception arg3)
- throws Exception {
- System.out.println("=======> my interceptor2:afterCompletion() ");
- }
- public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
- Object arg2, ModelAndView arg3) throws Exception {
- // TODO Auto-generated method stub
- System.out.println("=======> my interceptor2:postHandle() ");
- }
- public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
- Object arg2) throws Exception {
- System.out.println("=======> my interceptor2:preHandle()");
- return true;
- }
- }
2.在springmvc的配置文件中
- <mvc:interceptors>
- <mvc:interceptor>
- <mvc:mapping path="/**"/>
- <bean class="com.lifeng.interceptor.MyInterceptor1" />
- </mvc:interceptor>
- <mvc:interceptor>
- <mvc:mapping path="/**"/>
- <bean class="com.lifeng.interceptor.MyInterceptor2" />
- </mvc:interceptor>
- </mvc:interceptors>
3.执行顺序:
=======> my interceptor1:preHandle()
=======> my interceptor2:preHandle()
.......
=======> my interceptor2:postHandle()
=======> my interceptor1:postHandle()
=======> my interceptor2:afterCompletion()
=======> my interceptor1 :afterCompletion()