位置:首页 > > SpringMVC设置自定义拦截器

SpringMVC设置自定义拦截器

1.创建一个MyInterceptor类,并实现HandlerInterceptor接口
public class MyInterceptor implements HandlerInterceptor {
 
    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        System.out.println("afterCompletion");
    }
 
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        System.out.println("postHandle");
    }
 
    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        System.out.println("preHandle");
        return true;
    }
 
}
2.在SpringMVC的配置文件中配置
<!-- interceptor setting -->
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/mvc/**"/>
        <bean class="test.SpringMVC.Interceptor.MyInterceptor"></bean>
    </mvc:interceptor>        
</mvc:interceptors>

3.拦截器执行顺序