我是靠谱客的博主 柔弱抽屉,最近开发中收集的这篇文章主要介绍SpringMvc源码分析-DispatchServlet处理流程一、springmvc经典处理流程图 二、doDispatch方法三、总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、springmvc经典处理流程图

从网上找到了经典流程图,通过流程图来看,整个流程还是比较清晰的

具体方法处理流程图:

 二、doDispatch方法

 该方法是核心处理方法,里面主要做如下处理:

@SuppressWarnings("deprecation")
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); //检查是否为multipart请求
            multipartRequestParsed = (processedRequest != request);

            // Determine handler for the current request. 获取处理器映射器
            mappedHandler = getHandler(processedRequest);
            if (mappedHandler == null) {//没有找到映射器处理器 则报404 not found
                noHandlerFound(processedRequest, response);
                return;
            }

            // Determine handler adapter for the current request. 获取处理器适配器  通常是RequestMappingHandlerAdapter
            HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

            // Process last-modified header, if supported by the handler.
            String method = request.getMethod();
            boolean isGet = HttpMethod.GET.matches(method);
            if (isGet || HttpMethod.HEAD.matches(method)) { //判断是否为GET、HEAD请求
                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.
            // 处理器适配器 执行真正处理方法,在内部创建ModelAndView对象
            // 可能调用到AbstractHandlerMethodAdapter中的handle方法
            mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

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

            applyDefaultViewName(processedRequest, mv);//设置ModelAndView中视图名称
            mappedHandler.applyPostHandle(processedRequest, response, mv);//拦截器中postHandle方法
        }
        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);
            }
        }
    }
}

三、总结

处理流程比较清晰,下一篇介绍《SpringMvc源码分析-处理器映射器》

最后

以上就是柔弱抽屉为你收集整理的SpringMvc源码分析-DispatchServlet处理流程一、springmvc经典处理流程图 二、doDispatch方法三、总结的全部内容,希望文章能够帮你解决SpringMvc源码分析-DispatchServlet处理流程一、springmvc经典处理流程图 二、doDispatch方法三、总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部