<mvc:interceptors>spring
<!-- 声明自定义拦截器 -->数组
<bean id="firstHandlerInterceptor"mvc
class="com.atguigu.springmvc.interceptors.FirstHandlerInterceptor"></bean>ide
</mvc:interceptors>post
public class FirstHandlerInterceptor implements HandlerInterceptor {ui
@Overridethis
public void afterCompletion(HttpServletRequest arg0,spa
HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {.net
System.out.println(this.getClass().getName() + " - afterCompletion");xml
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println(this.getClass().getName() + " - postHandle");
}
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println(this.getClass().getName() + " - preHandle");
return true;
}
}
当有多个拦截器时, * preHandle:按照拦截器数组的正向顺序执行 * postHandle:按照拦截器数组的反向顺序执行 * afterCompletion:按照拦截器数组的反向顺序执行 * * 当多个拦截器的preHandle有不一样的值时 * 第一个返回false,第二个返回false:只有第一个preHandle会执行 * 第一个返回true,第二个返回false:两个(所有)拦截器的preHandle都会执行 * 可是(所有)postHandle都不会执行,而afterCompletion只有第一个(返回false的拦截器以前的全部afterCompletion)会执行 * 第一个返回false,第二个返回true:只有第一个的preHandle会执行