Spring mvc 视图处理逻辑

DispatcherServlet中

 initStrategies()

     initViewResolvers() 初始化了对应 ViewResolver

 doDispatch()

    applyDefaultViewName 没有返回视图的话,尝试 RequestToViewNameTranslator

    processDispatchResult()

    resolveViewName() 解析 View 对象

DispatcherServlet

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

   11

protected void initStrategies(ApplicationContext context) {

        initMultipartResolver(context);

        initLocaleResolver(context);

        initThemeResolver(context);

        initHandlerMappings(context);

        initHandlerAdapters(context);

        initHandlerExceptionResolvers(context);

        initRequestToViewNameTranslator(context);

        initViewResolvers(context);

        initFlashMapManager(context);

}

初始化view resolver

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

   11

   12

   13

   14

   15

   16

   17

   18

   19

   20

   21

   22

   23

   24

   25

   26

   27

   28

   29

   30

   31

   32

   33

   34

/**

 * Initialize the ViewResolvers used by this class.

 * <p>If no ViewResolver beans are defined in the BeanFactory for this

 * namespace, we default to InternalResourceViewResolver.

 */

private void initViewResolvers(ApplicationContext context) {

        ...

        if (this.detectAllViewResolvers) {

                // Find all ViewResolvers in the ApplicationContext, including ancestor contexts.

                Map<String, ViewResolver> matchingBeans =

                                BeanFactoryUtils.beansOfTypeIncludingAncestors(context, ViewResolver.class, true, false);

                if (!matchingBeans.isEmpty()) {

                        this.viewResolvers = new ArrayList<>(matchingBeans.values());

                        // We keep ViewResolvers in sorted order.

                        AnnotationAwareOrderComparator.sort(this.viewResolvers);

                }

        }

        else {

                try {

                        ViewResolver vr = context.getBean(VIEW_RESOLVER_BEAN_NAME, ViewResolver.class);

                        this.viewResolvers = Collections.singletonList(vr);

                }

                catch (NoSuchBeanDefinitionException ex) {

                        // Ignore, we'll add a default ViewResolver later.

                }

        }

        // Ensure we have at least one ViewResolver, by registering

        // a default ViewResolver if no other resolvers are found.

        if (this.viewResolvers == null) {

                this.viewResolvers = getDefaultStrategies(context, ViewResolver.class);

                ...

        }

}

doServices->doDispatch()

    1

    2

    3

    4

    5

    6

    7

    8

private void applyDefaultViewName(HttpServletRequest request, @Nullable ModelAndView mv) throws Exception {

        if (mv != null && !mv.hasView()) {

                String defaultViewName = getDefaultViewName(request);

                if (defaultViewName != null) {

                        mv.setViewName(defaultViewName);

                }

        }

}

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

   11

   12

   13

   14

   15

   16

   17

   18

   19

   20

   21

   22

   23

   24

   25

   26

   27

   28

   29

   30

   31

   32

   33

   34

   35

   36

   37

   38

   39

   40

private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,

                @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,

                @Nullable Exception exception) throws Exception {

        boolean errorView = false;

        if (exception != null) {

                if (exception instanceof ModelAndViewDefiningException) {

                        logger.debug("ModelAndViewDefiningException encountered", exception);

                        mv = ((ModelAndViewDefiningException) exception).getModelAndView();

                }

                else {

                        Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);

                        mv = processHandlerException(request, response, handler, exception);

                        errorView = (mv != null);

                }

        }

        // Did the handler return a view to render?

        if (mv != null && !mv.wasCleared()) {

                render(mv, request, response);

                if (errorView) {

                        WebUtils.clearErrorRequestAttributes(request);

                }

        }

        else {

                if (logger.isTraceEnabled()) {

                        logger.trace("No view rendering, null ModelAndView returned.");

                }

        }

        if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {

                // Concurrent handling started during a forward

                return;

        }

        if (mappedHandler != null) {

                mappedHandler.triggerAfterCompletion(request, response, null);

        }

}

  render the view

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

   11

   12

   13

   14

   15

   16

   17

   18

   19

   20

   21

   22

protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {

        // Determine locale for request and apply it to the response.

        。。。

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

        。。。

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FantasyBaby

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值