Spring MVC 拦截器 interceptor


一、两种方式来定义:

1.通过实现HandlerInterceptor接口,或继承HandlerInterceptor接口的实现类(如HandlerInterceptorAdapter)来定义。

2.通过实现WebRequestInterceptor接口,或继承WebRequestInterceptor接口的实现类来定义。

二、xml配置















三、处理过程

1.程序先执行preHandle()方法,如果该方法的返回值为true,则程序会继续向下执行处理器中的方法,否则将不再向下执行。

2.在业务处理器(即控制器Controller类)处理完请求后,会执行postHandle()方法,然后会通过DispatcherServlet向客户端返回响应。

3.在DispatcherServlet处理完请求后,才会执行afterCompletion()方法。

四、多拦截器处理过程

preHandle1 -> preHande2 -> 【Controller】 -> postHandle2 -> postHandle1 -> afterCompletion2 -> afterComplention1

五、支持

拦截器是spring容器的,是spring支持的

相关