<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會執行