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()
总结一下:
preHandle按拦截器定义顺序地调用
postHandle按拦截器定义逆序地调用
afterCompletion按拦截器定义逆序地调用
postHandle在截器链内所有拦截器返回成功调用
afterCompletion只有在preHandle返回true才调用
转载自:https://blog.csdn.net/u010509052/article/details/51120580