zoukankan      html  css  js  c++  java
  • 一次请求在SpringMVC核心执行流程

    一次请求在SpringMVC核心执行流程

     

    org.springframework.web.servlet.DispatcherServlet#doDispatch

    1.1 获取执行调用链HandlerExecutionChain
    1.2 获取处理器适配器HandlerAdapter
    1.3 调用拦截器preHandle
    1.4 调用适配器的handle方法
    1.5 处理视图名
    1.6 调用postHandle方法
    1.7 视图渲染

    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    boolean multipartRequestParsed = false;

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

    try {
    ModelAndView mv = null;
    Exception dispatchException = null;

    try {

    // 检查是否多模块请求
    processedRequest = checkMultipart(request);
    multipartRequestParsed = (processedRequest != request);

    // Determine handler for the current request.

    // 通过HandlerMapping根据url获取HandlerMethod, 再获取拦截器, 组合起来创建出调用执行链HandlerExecutionChain对象(HandlerMethod + 拦截器)
    mappedHandler = getHandler(processedRequest);
    if (mappedHandler == null) {
    noHandlerFound(processedRequest, response);
    return;
    }

    // Determine handler adapter for the current request.

    // 获取处理器适配器, 根据不同种类的适配器支持程度supports方法, 获取到合适的适配器, 处理的核心方法为handle方法
    // mappedHandler.getHandler()获取的是HandlerMethod对象
    HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

    // Process last-modified header, if supported by the handler.
    String method = request.getMethod();
    boolean isGet = "GET".equals(method);
    if (isGet || "HEAD".equals(method)) {
    long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
    return;
    }
    }

    // 处理拦截器preHandle方法
    if (!mappedHandler.applyPreHandle(processedRequest, response)) {
    return;
    }

    // Actually invoke the handler.

    // 调用HandlerAdapter的handle方法
    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

    if (asyncManager.isConcurrentHandlingStarted()) {
    return;
    }

    // 处理视图名字
    applyDefaultViewName(processedRequest, mv);

    // 处理拦截器postHandle方法
    mappedHandler.applyPostHandle(processedRequest, response, mv);
    }
    catch (Exception ex) {
    dispatchException = ex;
    }
    catch (Throwable err) {
    // As of 4.3, we're processing Errors thrown from handler methods as well,
    // making them available for @ExceptionHandler methods and other scenarios.
    dispatchException = new NestedServletException("Handler dispatch failed", err);
    }

    // 处理视图渲染
    processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
    }
    catch (Exception ex) {
    triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
    }
    catch (Throwable err) {
    triggerAfterCompletion(processedRequest, response, mappedHandler,
    new NestedServletException("Handler processing failed", err));
    }
    finally {
    if (asyncManager.isConcurrentHandlingStarted()) {
    // Instead of postHandle and afterCompletion
    if (mappedHandler != null) {
    mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
    }
    }
    else {
    // Clean up any resources used by a multipart request.
    if (multipartRequestParsed) {
    cleanupMultipart(processedRequest);
    }
    }
    }
    }
  • 相关阅读:
    SQL SERVER怎样将某个服务器上面的数据自动备份到另一台服务器上面(异地备份)
    jboss eap 6.2 ear包 下使用log4j日志
    配置jboss EAP 6.4 数据库连接超时时间
    java解析XML
    META-INF下文件读取
    Java J2EE读取配置文件
    EJB Remote/Local 绑定和JNDI Lookup
    Cypress web自动化27-Debugging调试你的代码
    Cypress web自动化26-mochawesome-merge合并json报告
    Cypress web自动化25-生成mochawesome-report合并报告
  • 原文地址:https://www.cnblogs.com/xiaoyusheng/p/13446412.html
Copyright © 2011-2022 走看看