一、SpringMVC的拦截器
1、写类implements HandlerInterceptor
public class MyMvcInterceptor implements HandlerInterceptor { //在client调用Controller之前执行(拦截request) //return true;执行Controller。return false;不执行Controller。 @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } //在Controller返回ModelAndView之前执行(拦截response) @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { } //运行完Controller方法之后执行 @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { return false; } }
2、在SpringMVC配置文件中配置
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/><!--表示拦截所有路径-->
<!--<mvc:exclude-mapping path=""/>表示排除哪些路径-->
<bean class="com.xxxx.MyMvcInterceptor"/> </mvc:interceptor> </mvc:interceptors>