有两种方法配置spring的拦截器
1. 实现接口: HandleInterceptor
public class MyInterceptor1 implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception { System.out.println("最后执行!!!一般用于释放资源!!"); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView model) throws Exception { System.out.println("Action执行之后,生成视图之前执行!!"); } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { System.out.println("action之前执行!!!"); return true; // 继续执行action } }
public class MyInterceptor2 extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor2.preHandle()"); return true; // 继续执行action } }
<!-- 配置自定义拦截器 --> <mvc:interceptors> <bean class="com.zdp.interceptor.MyInterceptor1"></bean> <!-- 拦截所有的url! --> <mvc:interceptor> <mvc:mapping path="/user/add" /> <!-- 只拦截添加用户 --> <bean class="com.zdp.interceptor.MyInterceptor2"></bean> </mvc:interceptor> </mvc:interceptors>
原文:http://blog.csdn.net/zdp072/article/details/38693049