• SpringMVC-拦截器参数及拦截器链配置


    目录

    一、拦截器参数

    二、拦截器链配置


    一、拦截器参数

    前置处理:

    1. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    2. System.out.println("preHandle...");
    3. return true;
    4. }

    参数:

    ■ request:请求对象

    ■ response:响应对象

    ■ handler:被调用的处理器对象,本质上是一个方法对象,对反射技术中的Method对象进行再包装

    返回值:

    ■ 返回值为false,被拦截的处理器将不执行

    后置处理:

    1. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    2. System.out.println("postHandle...");
    3. }

    参数:

    modeAanView:如果处理器执行完成具有返回结果,可以读取到对应数据与页面信息,并进行调整

    完成后处理:

    1. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    2. System.out.println("afterCompletion...");
    3. }

    参数:

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

    二、拦截器链配置

    配置第一个拦截器

    1. @Component
    2. //定义拦截器类,实现HandlerInterceptor接口
    3. //注意当前类必须受Spring容器控制
    4. public class ProjectInterceptor implements HandlerInterceptor {
    5. @Override
    6. //原始方法调用前执行的内容
    7. //返回值类型可以拦截控制的执行,true放行,false终止
    8. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    9. String contentType = request.getHeader("Content-Type");
    10. HandlerMethod hm = (HandlerMethod)handler;
    11. System.out.println("preHandle..."+contentType);
    12. return true;
    13. }
    14. @Override
    15. //原始方法调用后执行的内容
    16. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    17. System.out.println("postHandle...");
    18. }
    19. @Override
    20. //原始方法调用完成后执行的内容
    21. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    22. System.out.println("afterCompletion...");
    23. }
    24. }

     配置第二个拦截器

    1. @Component
    2. public class ProjectInterceptor2 implements HandlerInterceptor {
    3. @Override
    4. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    5. System.out.println("preHandle...222");
    6. return true;
    7. }
    8. @Override
    9. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    10. System.out.println("postHandle...222");
    11. }
    12. @Override
    13. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    14. System.out.println("afterCompletion...222");
    15. }
    16. }

    当配置多个拦截器时,形成拦截器链

    1. @Configuration
    2. @ComponentScan({"com.itheima.controller"})
    3. @EnableWebMvc
    4. //实现WebMvcConfigurer接口可以简化开发,但具有一定的侵入性
    5. public class SpringMvcConfig implements WebMvcConfigurer {
    6. @Autowired
    7. private ProjectInterceptor projectInterceptor;
    8. @Autowired
    9. private ProjectInterceptor2 projectInterceptor2;
    10. @Override
    11. public void addInterceptors(InterceptorRegistry registry) {
    12. //配置多拦截器
    13. registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
    14. registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");
    15. }
    16. }

     三个拦截器构成的拦截器链图例

     

    拦截器链的运行规律参照拦截器添加顺序为准

    当拦截器中出现对原始处理器的拦截,后面的拦截器均终止运行

    当拦截器运行中断,仅运行配置在前面的拦截器的afterCompletion操作

  • 相关阅读:
    成都睿趣科技:抖音开店初期要注意什么
    latex三线表使用辅助线,超出表格长度
    Apache 基金会年度报告 | ShardingSphere 代码提交量位列前十
    似然和概率
    vue组件间通信
    项目技术复盘
    Mysql内外连接
    【学习笔记】4、组合逻辑电路(下)
    高云FPGA系列教程(6):ARM定时器使用
    Spring – 记录传入请求
  • 原文地址:https://blog.csdn.net/m0_61961937/article/details/125612252