美文网首页
Spring mvc执行流程图-1

Spring mvc执行流程图-1

作者: engineer_tang | 来源:发表于2020-08-23 22:01 被阅读0次

1. Spring mvc总体流程图

DispatcherServlet是Spring mvc框架控制处理中心,所有的请求调用的入口和返回都从这里进出。下面将对DispatcherServlet作详细说明,以及其执行流程进行解剖。

1.1 Spring mvc的执行流程图

image.png

Spring mvc的执行流程大致如下:
1)所有请求被DispatcherServlet控制器拦截。
2)被拦截的请求去handlerMappings中寻找对应的HandlerMapping对象并得到请求对应的Handler对象。
3)把获取到的Handler对象以及根据请求查找跟请求对应的拦截器作为入参,封装成一个HandlerExectionChain对象,返回到DispatcherServlet控制器。
4)把HandlerExecutionChain对象作为入参,去handlerAdapters集合中寻找对应的HandlerAdapter对象。
5)把HandlerExectionChain对象作为入参调用HandlerAdapter对象的handle方法,执行完毕将返回ModelAndView对象到DispatcherServlet控制器中。
6)从viewResolvers集合中查找对应的View对象,并返回给到DispatcherServlet控制器。
7)View视图渲染成具体的文件格式并返回给客户端。

2. DispatcherServlet类详细解析

image.png

View视图渲染

    protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // Determine locale for request and apply it to the response.
        Locale locale =
                (this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale());
        response.setLocale(locale);

        View view;
        String viewName = mv.getViewName();
        if (viewName != null) {
            // We need to resolve the view name.
            view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
            if (view == null) {
                throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
                        "' in servlet with name '" + getServletName() + "'");
            }
        }
        else {
            // No need to lookup: the ModelAndView object contains the actual View object.
            view = mv.getView();
            if (view == null) {
                throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
                        "View object in servlet with name '" + getServletName() + "'");
            }
        }

        // Delegate to the View object for rendering.
        if (logger.isTraceEnabled()) {
            logger.trace("Rendering view [" + view + "] ");
        }
        try {
            if (mv.getStatus() != null) {
                response.setStatus(mv.getStatus().value());
            }
            view.render(mv.getModelInternal(), request, response);
        }
        catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Error rendering view [" + view + "]", ex);
            }
            throw ex;
        }
    }

render具体源码

    @Override
    public void render(@Nullable Map<String, ?> model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        if (logger.isDebugEnabled()) {
            logger.debug("View " + formatViewName() +
                    ", model " + (model != null ? model : Collections.emptyMap()) +
                    (this.staticAttributes.isEmpty() ? "" : ", static attributes " + this.staticAttributes));
        }

        Map<String, Object> mergedModel = createMergedOutputModel(model, request, response);
        prepareResponse(request, response);
        renderMergedOutputModel(mergedModel, getRequestToExpose(request), response);
    }

View的类型如下图(ctrl+h可查看类的所有子类)


image.png

相关文章

网友评论

      本文标题:Spring mvc执行流程图-1

      本文链接:https://www.haomeiwen.com/subject/lzkwjktx.html