SpringMvc自定义拦截器

Spring Web MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。


实现HandlerInterceptor接口,如下:


SpringMvc自定义拦截器_第1张图片


拦截器配置

1.    针对某种mapping配置拦截器


SpringMvc自定义拦截器_第2张图片

2.    针对所有mapping配置全局拦截器


SpringMvc自定义拦截器_第3张图片

正常流程测试

定义两个拦截器分别为:HandlerInterceptor1和HandlerInteptor2,每个拦截器的preHandler方法都返回true。




运行流程

HandlerInterceptor1..preHandle..

HandlerInterceptor2..preHandle..

HandlerInterceptor2..postHandle..

HandlerInterceptor1..postHandle..

HandlerInterceptor2..afterCompletion..

HandlerInterceptor1..afterCompletion..

总结:

preHandle按拦截器定义顺序调用

postHandler按拦截器定义逆序调用

afterCompletion按拦截器定义逆序调用

postHandler在拦截器链内所有拦截器返成功调用

afterCompletion只有preHandle返回true才调用

你可能感兴趣的:(SpringMvc自定义拦截器)