用于拦截请求,过滤后再拦截
实现HandlerInterceptor接口
配置拦截器
package cn.zys.lanjieqi; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class MyZhujieqi implements HandlerInterceptor{ //在请求处理方法之前执行 //如果返回true 执行下一个拦截器 //如果返回false 就不执行下一个 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub System.out.println("处理前~~"); return true; } //在请求处理方法之后执行 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub System.out.println("处理后~~"); HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); } //DispatcherServlet 处理后执行 --适合做一些清理工作 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } }
配置mvc.xml
<!-- 拦截器的配置 --> <mvc:interceptors> <!-- 配置一个拦截器 --> <mvc:interceptor> <!-- /**此路径下的所有子路径 --> <mvc:mapping path="/**"/> <bean class="cn.zys.lanjieqi.MyZhujieqi"></bean> </mvc:interceptor> </mvc:interceptors>