Spring mvc DispatcherServlet-view rendering process
Now let's explain the last step of DispatcherServletDispatcherServlet: view rendering. The view rendering process is after obtaining ModelAndView. View rendering process: DispatcherServlet. java doService () ---> doDispatch () ---> processDispatchResult () ---> render () processDispatchResult (): mainly handles exceptions, request statuses, and trigger request Completion events, rendering of the image is handed over to render (). the render () rendering process is as follows: 1. judge whether the view in ModelAndView is the view name and does not obtain its instance object. If it is based on the name, call resolveViewName to obtain the corresponding View object from the view parser; otherwise, use the getview method in ModelAndView to obtain the view object. 2. Call the render () method of view. The specific code is as follows: copy the code/*** 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 v Iew */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. getModelInt Ernal (), locale, request); if (view = null) {throw new ServletException ("cocould 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 r Endering view ["+ view +"] in DispatcherServlet with name' "+ getServletName () +" '", ex) ;} throw ex ;}} copy the code, then how is the view rendered? Let's take a look at the definition of view: copy the code org. springframework. web. servletInterface View All Known Subinterfaces: SmartView All Known Implementing Classes: Values, AbstractExcelView, AbstractFeedView, values, abstractshortview, values, AbstractTemplateView, values, AbstractView, Confi Metrics, FreeMarkerView, InternalResourceView, metrics, JasperReportsXlsView, JstlView, metrics, metrics, MarshallingView, RedirectView, TilesView, TilesView, metrics, metrics, VelocityView, and XsltView metrics ----------------------------------------- --------------------------------------- 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 ma Y differ widely. an obvious implementation wocould 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 shocould be beans. they are likely to be instantiated as beans by a ViewResolver. as this interface is stateless, view implementations shoshould be thread-safe. copy the code spring For so many views, there are certainly many view parsers: copy the code org. springframework. web. servletInterface ViewResolver All Known Implementing Classes: Listener, listener, BeanNameViewResolver, listener, listener, InternalResourceViewResolver, listener, ResourceBundleViewResolver, TilesViewResolver, supervisor ElocityLayoutViewResolver, VelocityViewResolver, XmlViewResolver, effectviewresolver 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 vi Ews 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. copy the Code. For the InternalResourceViewResolver and InternalResourceView provided by JSP. Let's take a look at what the render method of view looks like? According to the inheritance relationship of InternalResourceView: org. springframework. web. servlet. view. abstractVieworg. springframework. web. servlet. view. abstractUrlBasedVieworg. springframework. web. servlet. view. internalResourceView finally finds the render method in AbstractView, as shown in the following code: copy the code/*** Prepares the view given the specified model, merging it with static * attributes and a RequestContext attribute, if necessary. * Delegates to renderMergedOutputM Odel 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);} the process of copying the code is as follows: create a map with dynamic values and static attributes; set the response Message Header; put the rendering view work in the implementation of renderMergedOutputModel, this is left to InternalResourceView for implementation. Let's take a look at this implementation: copy the code/*** Render the internal resource given the specified model. * This includes des 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 = get RequestToExpose (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 ("cocould 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 sup Posed to determine the content type itself. if (logger. isDebugEnabled () {logger. debug ("Forwarding to resource [" + getUrl () + "] in InternalResourceView '" + getBeanName () + "'");} rd. forward (requestToExpose, response) ;}} the process of copying code can be summarized into the following steps: 1. encapsulate the request for use by RequestDispatcher; 2. put attributes and values in map as attributes into the packaged request; 3. put the helper of different implementation classes into the packaged request; 4. prepare before rendering, and determine the path of request dispatcher to jump (or inclue) 5. GET request Dispatcher. 6. Whether the request contains the include uri attribute is actually the forward or include method. Forward is a servlet, JSP file, or HTML file that jumps to the server. Parameters des the content of a resource (servlet, JSP page, HTML file) in the response. summary: we can see that the rendering process of the view is to wrap the model into a map form and bring it to the server through the request attribute.