• 对HandlerExecutionChain类的理解分析


    HandlerExecutionChain类比较简单,好理解。

    ========================================================================

    /*
    * 处理器执行链由处理器对象和拦截器组成。
    */
    public
    class HandlerExecutionChain {

    ========================================================================

    下面是类的部分属性。

        private final Object handler;  //处理器对象。
    
        private HandlerInterceptor[] interceptors; //拦截器数组
    
        private List<HandlerInterceptor> interceptorList; //拦截器列表
    

    ========================================================================

        /**
         * Apply preHandle methods of registered interceptors.
         * @return {@code true} if the execution chain should proceed with the
         * next interceptor or the handler itself. Else, DispatcherServlet assumes
         * that this interceptor has already dealt with the response itself.
    * 执行已经注册的拦截的 preHandle()方法。如果返回true,则执行链可以执行下一个拦截器的preHandle()方法或 handler 自身。
    * 否则,
    */ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; }

    ========================================================================

        /*
    * 执行已经注册的拦截器 postHandle()方法。
    */ void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = interceptors.length - 1; i >= 0; i--) { HandlerInterceptor interceptor = interceptors[i]; interceptor.postHandle(request, response, this.handler, mv); } } }

    ========================================================================

        /**
         * 这个方法只会执行preHandle()方法已经成功执行并且返回true的拦截器中的postHandle()方法。
         */
        void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)
                throws Exception {
    
            HandlerInterceptor[] interceptors = getInterceptors();
            if (!ObjectUtils.isEmpty(interceptors)) {
                for (int i = this.interceptorIndex; i >= 0; i--) {
                    HandlerInterceptor interceptor = interceptors[i];
                    try {
                        interceptor.afterCompletion(request, response, this.handler, ex);
                    }
                    catch (Throwable ex2) {
                        logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
                    }
                }
            }
        }

    ========================================================================

    ========================================================================

    ========================================================================

    ========================================================================

    ========================================================================

    ========================================================================

  • 相关阅读:
    用XPath定位Web页面元素时,如何快速验证XPath语句是否正确?
    遇到Web页面禁用鼠标右键操作时,该如何解禁?
    Python之win32模块
    Python之smtplib模块
    Python之telnetlib模块
    使用HttpRunner3+Allure+Jenkins实现Web接口自动化测试
    Python实现Thrift Server
    Python之requests模块-hook
    php新手常用的函数(随时更新)
    手机测试pc端网页
  • 原文地址:https://www.cnblogs.com/GooPolaris/p/7114367.html
Copyright © 2020-2023  润新知