首页 > 代码库 > Spring MVC 学习 之 - 拦截器

Spring MVC 学习 之 - 拦截器

public class GlobalInterceptor  implements HandlerInterceptor {    public boolean preHandle(HttpServletRequest request,            HttpServletResponse response, Object handler) throws Exception {                System.out.println("GlobalInterceptor 开始调用");                return true;    }    public void postHandle(HttpServletRequest request,            HttpServletResponse response, Object handler,            ModelAndView modelAndView) throws Exception {        // TODO Auto-generated method stub        System.out.println("GlobalInterceptor 调用中");            modelAndView.addObject("msg", "GlobalInterceptor 传入");    }    public void afterCompletion(HttpServletRequest request,            HttpServletResponse response, Object handler, Exception ex)            throws Exception {        // TODO Auto-generated method stub        System.out.println("GlobalInterceptor 调用完成");    }}

 

 添加配置:

     全局URL拦截:

<mvc:interceptors>      <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->    <bean class="com.rhythmk.web1.filter.GlobalInterceptor"  />  </mvc:interceptors> 

 

    指定规格URL拦截:

<mvc:interceptors>      <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->    <mvc:interceptor>          <mvc:mapping path="/uic/*"/>         <!--  定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的   -->        <bean class="com.rhythmk.web1.filter.GlobalInterceptor"/>      </mvc:interceptor>  </mvc:interceptors> 

 

Spring MVC 学习 之 - 拦截器