整个spring mvc的架构如下图所示:

现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染。视图渲染的过程是在获取到ModelAndView后的过程。

视图渲染的过程:

DispatcherServlet.java

doService()--->doDispatch()--->processDispatchResult()--->render()

processDispatchResult():主要处理异常、请求状态及触发请求完成事件,图的渲染工作交给了render().

render()渲染过程如下:

1. 判断ModelAndView中view是否为view name,没有获取其实例对象:如果是根据name,如果是则需要调用resolveViewName从视图解析器获取对应的视图(View)对象;否则ModelAndView中使用getview方法获取view对象。

2. 然后调用view的render()方法。

代码如下:

 /**
* Render the given ModelAndView.
* <p>This is the last stage in handling a request. It may involve resolving the view by name.
* @param mv the ModelAndView to render
* @param request current HTTP servlet request
* @param response current HTTP servlet response
* @throws ServletException if view is missing or cannot be resolved
* @throws Exception if there's a problem rendering the 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.resolveLocale(request);
response.setLocale(locale); View view;
if (mv.isReference()) {
// We need to resolve the view name.
view = resolveViewName(mv.getViewName(), 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.isDebugEnabled()) {
logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'");
}
try {
view.render(mv.getModelInternal(), request, response);
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" +
getServletName() + "'", ex);
}
throw ex;
}
}

那么view 是如何渲染的?我们来看看view的定义:

 org.springframework.web.servlet
Interface View All Known Subinterfaces:
SmartView
All Known Implementing Classes:
AbstractAtomFeedView, AbstractExcelView, AbstractFeedView, AbstractJasperReportsSingleFormatView, AbstractJasperReportsView, AbstractJExcelView, AbstractPdfStamperView, AbstractPdfView, AbstractRssFeedView, AbstractTemplateView, AbstractUrlBasedView, AbstractView, ConfigurableJasperReportsView, FreeMarkerView, InternalResourceView, JasperReportsCsvView, JasperReportsHtmlView, JasperReportsMultiFormatView, JasperReportsPdfView, JasperReportsXlsView, JstlView, MappingJackson2JsonView, MappingJacksonJsonView, MarshallingView, RedirectView, TilesView, TilesView, VelocityLayoutView, VelocityToolboxView, VelocityView, XsltView -------------------------------------------------------------------------------- public interface ViewMVC View for a web interaction. Implementations are responsible for rendering content, and exposing the model. A single view exposes multiple model attributes.
This class and the MVC approach associated with it is discussed in Chapter 12 of Expert One-On-One J2EE Design and Development by Rod Johnson (Wrox, 2002). View implementations may differ widely. An obvious implementation would be JSP-based. Other implementations might be XSLT-based, or use an HTML generation library. This interface is designed to avoid restricting the range of possible implementations. Views should be beans. They are likely to be instantiated as beans by a ViewResolver. As this interface is stateless, view implementations should be thread-safe.

spring提供了如此多的视图,那么肯定的是也会有很多视图解析器:

 org.springframework.web.servlet
Interface ViewResolver All Known Implementing Classes:
AbstractCachingViewResolver, AbstractTemplateViewResolver, BeanNameViewResolver, ContentNegotiatingViewResolver, FreeMarkerViewResolver, InternalResourceViewResolver, JasperReportsViewResolver, ResourceBundleViewResolver, TilesViewResolver, TilesViewResolver, UrlBasedViewResolver, VelocityLayoutViewResolver, VelocityViewResolver, XmlViewResolver, XsltViewResolver
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. -------------------------------------------------------------------------------- public interface ViewResolverInterface to be implemented by objects that can resolve views by name.
View state doesn't change during the running of the application, so implementations are free to cache views. Implementations are encouraged to support internationalization, i.e. localized view resolution.

其中,针对JSP提供的InternalResourceViewResolver与InternalResourceView。

我们先看一下view的render方法是什么样子的?

根据InternalResourceView的继承关系:


最终找到render方法在AbstractView中,如下代码所示:

 /**
* Prepares the view given the specified model, merging it with static
* attributes and a RequestContext attribute, if necessary.
* Delegates to renderMergedOutputModel for the actual rendering.
* @see #renderMergedOutputModel
*/
@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (logger.isTraceEnabled()) {
logger.trace("Rendering view with name '" + this.beanName + "' with model " + model +
" and static attributes " + this.staticAttributes);
} Map<String, Object> mergedModel = createMergedOutputModel(model, request, response);
prepareResponse(request, response);
renderMergedOutputModel(mergedModel, request, response);
}

流程如下:

创建一个动态值和静态属性的map;

设置response 报文头;

把渲染view的工作放到renderMergedOutputModel()实现中,这个留给InternalResourceView来实现。

我们看看这个实现:

 /**
* Render the internal resource given the specified model.
* This includes setting the model as request attributes.
*/
@Override
protected void renderMergedOutputModel(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { // Determine which request handle to expose to the RequestDispatcher.
HttpServletRequest requestToExpose = getRequestToExpose(request); // Expose the model object as request attributes.
exposeModelAsRequestAttributes(model, requestToExpose); // Expose helpers as request attributes, if any.
exposeHelpers(requestToExpose); // Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(requestToExpose, response); // Obtain a RequestDispatcher for the target resource (typically a JSP).
RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath);
if (rd == null) {
throw new ServletException("Could not get RequestDispatcher for [" + getUrl() +
"]: Check that the corresponding file exists within your web application archive!");
} // If already included or response already committed, perform include, else forward.
if (useInclude(requestToExpose, response)) {
response.setContentType(getContentType());
if (logger.isDebugEnabled()) {
logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
rd.include(requestToExpose, response);
} else {
// Note: The forwarded resource is supposed to determine the content type itself.
if (logger.isDebugEnabled()) {
logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
rd.forward(requestToExpose, response);
}
}

流程可以归纳为以下几步:

1. 包装request,供RequestDispatcher来使用;

2. 将map中的属性和值作为属性放入包装的request;

3. 将不同实现类的helper放入包装的request中;

4. 渲染前的准备,确定request dispatcher要跳向(或者inclue)的路径

5. 获取request dispatcher。

6. 根据request中是否包含include uri属性来确实是forward或者include方法。

forward是跳向服务器的servlet, JSP文件, 或者 HTML文件。

Includes the content of a resource (servlet, JSP page,HTML file) in the response.

注意,在上述流程中出现了RequestDispatcher,那么这类的作用是什么呢?

 getRequestDispatcher

 RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher. The difference between this method and ServletContext#getRequestDispatcher is that this method can take a relative path.

简洁的来说,

1. RequestDispatcher 是一个包装器,它将制定路径的(静态或者动态)资源包装起来。RequestDispatcher 可以用于将一个请求分发给指定的资源或者包裹响应报文中的资源。

2. RequestDispatcher 的获取,有这种形式,一种使用ServletRequest.getRequestDispatcher(java.lang.String path). 另一种是servletContext.getRequestDispatcher(java.lang.String path);不同之处在于:前面的方法支持相对路径,以'/'作为当前上下文的跟路径;后一种不支持后一种不支持相对路径。

可以看到视图的渲染过程是把model包装成map形式通过request的属性带到服务器端。

SpringMVC DispatcherServlet-------视图渲染过程的更多相关文章

  1. spring mvc DispatcherServlet详解之四---视图渲染过程

    整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程 ...

  2. SpringMVC核心——视图渲染(包含视图解析)问题

    一.本来想说的是返回值处理问题,但在 SpringMVC 中,返回值处理问题的核心就是视图渲染.所以这里标题叫视图渲染问题. 本来想在上一篇文章中对视图解析进行说明的,但是通过源码发现,它应该算到视图 ...

  3. 微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染

    § 详情 - 数据渲染 本文配套视频地址: https://v.qq.com/x/page/x055550lrvd.html 开始前请把 ch4-2 分支中的 code/ 目录导入微信开发工具 这一节 ...

  4. SpringMVC之四:渲染Web视图

    理解视图解析 在前面的例子中,我们看到控制器返回的都是一个逻辑视图的名称,然后把这个逻辑视图名称交给view resolver,然后返回渲染后的 html 页面给 client. 将控制器中请求处理的 ...

  5. Spring框架系列(13) - SpringMVC实现原理之DispatcherServlet的初始化过程

    前文我们有了IOC的源码基础以及SpringMVC的基础,我们便可以进一步深入理解SpringMVC主要实现原理,包含DispatcherServlet的初始化过程和DispatcherServlet ...

  6. Yii2.0源码阅读-视图(View)渲染过程

    之前的文章我们根据源码的分析,弄清了Yii如何处理一次请求,以及根据解析的路由如何调用控制器中的action,那接下来好奇的可能就是,我在控制器action中执行了return $this->r ...

  7. (二)SpringMVC之执行的过程

    (DispatcherServlet在Spring当中充当一个前端控制器的角色,它的核心功能是分发请求.请求会被分发给对应处理的Java类,Spring MVC中称为Handle.) ①   用户把请 ...

  8. 学习SpringMVC——说说视图解析器

    各位前排的,后排的,都不要走,咱趁热打铁,就这一股劲我们今天来说说spring mvc的视图解析器(不要抢,都有位子~~~) 相信大家在昨天那篇如何获取请求参数篇中都已经领略到了spring mvc注 ...

  9. SpringMVC重定向视图RedirectView小分析

    目录 前言 RedirectView介绍 实例讲解 总结 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnbl ...

随机推荐

  1. AutomaticInteger中CAS运用分析

    摘要 在接触CAS的时候虽然对它流程了解了但是对其如何解决并发问题还是一直有疑问的,所以在就选择了java中典型线程安全的AtomicInteger类进行了源码的分析. CAS简介 CAS的全称为co ...

  2. MATLAB复制图片时边框大的问题

    当使用MATLAB画图时,需要将图片复制到word中,会发现图片有一个白色的边框,在论文的排版中是一个影响美观的问题 例如: >> x = 0:10; >> y = sin(x ...

  3. 报错android.view.InflateException: Binary XML file line #11: Attempt to invoke virtual method 'boolean

    出现这种问题,打开Android monitor的调试信息发现是 android.view.InflateException: Binary XML file line #11: Attempt to ...

  4. KClient——kafka消息中间件源码解读

    目录 kclient消息中间件 kclient-processor top.ninwoo.kclient.app.KClientApplication top.ninwoo.kclient.app.K ...

  5. windows c++如何使窗口动态改变位置

    在windows软件中,经常会碰到一个功能:鼠标hover在某个地方时会出现窗口,有时候这个窗口的位置是会动态调整的. 熟悉使用windows API,理解windows中虚拟坐标.工作区坐标.屏幕坐 ...

  6. cobbler部署以及使用

    常用软件安装及使用目录 资源链接:https://pan.baidu.com/s/1yfVnuSgY5vOTh-B74tpVyw   网盘分享的文件在此 cobbler第一次操作history. ec ...

  7. 第十次ScrumMeeting博客

    第十次ScrumMeeting博客 本次会议于11月5日(日)22时整在新主楼G座2楼召开,持续20分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 特邀嘉宾:陈彦吉学长. 1. 每个人 ...

  8. iOS静态库.a总结(2017.1.24增加脚本打包方法)

    修改于:2017.1.24 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.根据源代码的公开情况,库可以分为2种类型 a.开源库 公开源代码,能看到具体实现 ,比如SDWebImag ...

  9. check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc

    往一个新建的MySQL表中插入数据报错 2018-08-22 14:09:18.875 ERROR 9256 --- [apr-8080-exec-9] o.s.b.w.servlet.support ...

  10. Teamproject Week7 --Scrum Meeting #1 2014.10.28

    这是团队的第一次会议,具体议题如下: 1)我们明确了团队成员的职责所需: PM职责:根据项目范围.质量.时间与成本的综合因素的考虑,进行项目的总体规划与阶段计划.  控制项目组各成员的工作进度,即时了 ...