/**
* 拦截器,实现HandlerInterceptor接口
* 只拦截控制器,不拦截jsp
* @author Administrator
*
*/
public class MyHeaderInterceptor implements HandlerInterceptor{
//preHandle在Controller之前执行
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("Interceptor:preHandle");
return true;
}
//postHandle在控制器之后,jsp之前执行
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("Interceptor:postHandle");
}
//afterCompletion最后一定会执行,
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("Interceptor:afterCompletion");
}
}
SpringMVC.xml
<!-- 拦截所有控制器 -->
<mvc:interceptors>
<bean id="interceptor" class="com.ment.filter.MyHeaderInterceptor"></bean>
</mvc:interceptors>
<!-- 只拦截mian控制器,可以配置多个mvc:mapping -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/main"/>
<bean id="interceptor" class="com.ment.filter.MyHeaderInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>