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

视图渲染的过程:

DispatcherServlet.java

servlet渲染jspspring mvc DispatcherServlet详解之四视图衬着进程 GraphQL

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.servletInterface ViewAll 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.servletInterface ViewResolverAll 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的继续关系:

org.springframework.web.servlet.view.AbstractView

org.springframework.web.servlet.view.AbstractUrlBasedView

org.springframework.web.servlet.view.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,那么这类的浸染是什么呢?

getRequestDispatcherRequestDispatcher 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的属性带到做事器端。