springmvc 接收到一个请求之后,底层源码是如何执行的

springmvc 一个请求访问之后的过程源码分析

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.
                mappedHandler = getHandler(processedRequest);
                if (mappedHandler == null || mappedHandler.getHandler() == null) {
                    noHandlerFound(processedRequest, response);
                    return;
                }

                // Determine handler adapter for the current request.
                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 (logger.isDebugEnabled()) {
                        logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
                    }
                    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
                        return;
                    }
                }

                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                    return;
                }

                // Actually invoke the handler.
                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

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

                applyDefaultViewName(processedRequest, mv);
                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);
                }
            }
        }
    }

1、httpServletRequest请求进入之后首先去web.xml中,找到定义的dispatcherServlet类。进入这个类。

2、其实dispatcherServlet中最关键的方法是doDispatch,进入的请求回去访问这个方法。

3、通过getHandler返回一个HandlerExecutionChain对象,其实也就是我们所说的根据requestMapping获取到handler。

4、继续执行代码,判断,当HandlerExecutionChain不为空的时候,继续执行代码。通过getHandlerAdapter方法(参数是之前获取到的HandlerExecutionChain)获取到一个HandlerAdapter对象。也就是我们所说的处理器适配器。因为HandlerAdapter是一个list集合,所以要根据不同过的handler规则适配到合适的HandlerAdapter。

5、通过HandlerAdapter的getLastModified判断HandlerExecutionChain是否是一个无效的handler

6、通过HandlerAdapter对象的handle方法(参数是之前获取的HandlerExecutionChain)执行获取到ModelAndView对象。

7、通过HandlerExecutionChain对象的applyPostHandle(参数是modelandview对象),中的拦截器判断,查看该请求是否有访问权限。

8、执行processDispatchResult方法,使得response返回ModelAndView

   1、判断ModelAndView不为空,执行render方法。获取ModelAndView对象的view值和model值。

   2、通过执行上一步获取到的View对象的render方法(参数是ModelMap,其实说白了就是上一步获取到的model值的map)并且渲染页面。也就是说,页面的渲染是从执行了render方法之后才完成的。

   3、最后在通过mappedHandler.triggerAfterCompletion判断该handler请求有没有权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值