详解SpringMVC的拦截器参数及拦截器链配置

目录

  • 一、拦截器参数
  • 二、拦截器链配置

一、拦截器参数 前置处理
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle..."); return true; }

参数:
■ request:请求对象
■ response:响应对象
■ handler:被调用的处理器对象,本质上是一个方法对象,对反射技术中的Method对象进行再包装
返回值:
■ 返回值为false,被拦截的处理器将不执行
【详解SpringMVC的拦截器参数及拦截器链配置】后置处理
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle..."); }

参数:
modeAanView:如果处理器执行完成具有返回结果,可以读取到对应数据与页面信息,并进行调整
完成后处理
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion..."); }

参数:
ex:如果处理器执行过程中出现异常对象,可以针对异常情况进行单独处理

二、拦截器链配置 配置第一个拦截器
@Component//定义拦截器类,实现HandlerInterceptor接口//注意当前类必须受Spring容器控制public class ProjectInterceptor implements HandlerInterceptor {@Override//原始方法调用前执行的内容//返回值类型可以拦截控制的执行,true放行,false终止public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String contentType = request.getHeader("Content-Type"); HandlerMethod hm = (HandlerMethod)handler; System.out.println("preHandle..."+contentType); return true; } @Override//原始方法调用后执行的内容public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle..."); } @Override//原始方法调用完成后执行的内容public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion..."); }}

配置第二个拦截器
@Componentpublic class ProjectInterceptor2 implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle...222"); return true; } @Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...222"); } @Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...222"); }}

当配置多个拦截器时,形成拦截器链
@Configuration@ComponentScan({"com.itheima.controller"})@EnableWebMvc//实现WebMvcConfigurer接口可以简化开发,但具有一定的侵入性public class SpringMvcConfig implements WebMvcConfigurer {@Autowiredprivate ProjectInterceptor projectInterceptor; @Autowiredprivate ProjectInterceptor2 projectInterceptor2; @Overridepublic void addInterceptors(InterceptorRegistry registry) {//配置多拦截器registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*"); registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*"); }}

三个拦截器构成的拦截器链图例
详解SpringMVC的拦截器参数及拦截器链配置
文章图片

详解SpringMVC的拦截器参数及拦截器链配置
文章图片

拦截器链的运行规律参照拦截器添加顺序为准
当拦截器中出现对原始处理器的拦截,后面的拦截器均终止运行
当拦截器运行中断,仅运行配置在前面的拦截器的afterCompletion操作
以上就是详解SpringMVC的拦截器参数及拦截器链配置的详细内容,更多关于SpringMVC拦截器配置的资料请关注脚本之家其它相关文章!

    推荐阅读