SpringMvc - 拦截器
springmvc-servlet.xml
<!--配置拦截器--> <mvc:interceptors> <mvc:interceptor> <!--拦截user下的所有请求 /user/**--> <mvc:mapping path="/user/**"/> <!--不拦截user下的/user/add请求--> <mvc:exclude-mapping path="/user/add"/> <!--注册拦截器--> <bean class="com.levi.config.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors>
com.levi.config.LoginInterceptor
/* * 拦截器 * 实现HandlerInterceptor 重写方法 * */ public class LoginInterceptor implements HandlerInterceptor { /*请求执行之前执行*/ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("请求执行之前 执行"); /*true:放行 false:拦截*/ return true; } /*请求执行之后执行*/ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("请求执行之后 执行"); } /*请求执行 postHandle方法执行完之后执行 清理的作用*/ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("清理"); } }