1:配置(applicationContext.xml中):
1 <!-- 拦截器配置 --> 2 <mvc:interceptors> 3 <mvc:interceptor> 4 <!-- 包括这个请求下面的所有的请求 --> 5 <mvc:mapping path="/**"/> 6 <bean class="com.lieyan.config.MyInterceptor"/> 7 </mvc:interceptor> 8 </mvc:interceptors>
2:实现例子(controller中的方法里):
1 package com.lieyan.config; 2 3 import org.springframework.web.servlet.HandlerInterceptor; 4 import org.springframework.web.servlet.ModelAndView; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class MyInterceptor implements HandlerInterceptor { 10 11 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 12 13 System.out.println("处理前============================"); 14 15 return true; 16 } 17 18 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 19 System.out.println("处理后============================"); 20 } 21 22 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 23 System.out.println("清理============================"); 24 } 25 }