欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

对handlerexecutionchain类的深入理解

程序员文章站 2023-12-14 19:15:52
handlerexecutionchain类比较简单,好理解。 /* * 处理器执行链由处理器对象和拦截器组成。 */ public class han...

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);
        }
      }
    }
  }

以上这篇对handlerexecutionchain类的深入理解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: