Springmvc Dispatchservlet Initialize nine load strategies (iii)

Source: Internet
Author: User
Tags locale

7. Initrequesttoviewnametranslator Request View Name

It is mainly related to view resolution, if the Viewresolvers, Modelandview, view, etc. do not have much impression, you can first look at the 8th section.

The requesttoviewnametranslator is primarily to get the viewname in the request, and then the Modelandview object can be obtained based on this viewname.

Requesttoviewnametranslator Interface Definition:

Public interface Requesttoviewnametranslator {//strategy interface for translating an incoming httpservletrequest into a Logical view name when no view name is explicitly supplied. String Getviewname (HttpServletRequest request) throws Exception;}

The simple thing is to assemble the view name according to request requests, that's all.

The default view name is set in the Dodispatch function of Dispatcherservlet:

Sets the default view name Applydefaultviewname (processedrequest, MV), and Applydefaultviewname determines if Modelandview's Hasview is empty, Set Viewname:if (mv! = null &&!mv.hasview ()) {    mv.setviewname (getdefaultviewname (Request));}

  

8. Initviewresolvers View Parser

Viewresolvers is a list<viewresolver> type of data, view parsing is chained, and if a view parser does not find a corresponding view based on the viewname and local parameters, it starts using the second parser to query. Until it is found. The default viewresolvers is Internalresourceviewresolver:

Org.springframework.web.servlet.viewresolver=org.springframework.web.servlet.view.internalresourceviewresolver

The function of Viewresolver is to return a View object by parsing viewname:

Public interface Viewresolver {    View resolveviewname (String viewName, locale locale) throws Exception;}

The previous article indicates that Handleradapter handles handler requests and returns a View object Modelandview. If the view object of the Mdoelandview object class is a string instance, the Resolveviewname method of the class is implemented by the Viewresolver interface to convert to the view type, if Mdoelandview The object class's view object is a Viwe instance, and the Getter method allows you to get the View object.

The properties of the Modelandview class are defined as follows:

public class Modelandview {    //Two set mode, 1:string type  2:view type. by IsReference () to differentiate    //If view is a string type, the Resolveviewname method of the class is implemented through the Viewresolver interface to convert to the view type    private Object view;    linkedhashmap<string, object> type    private modelmap model;    Private httpstatus status;    Private Boolean cleared = false;    ...    }

After you get to the View object, call view's Render method for page rendering.

The specific process is as follows:

When the request arrives Dodispatch Handleradapte processes the request, returns a Mdoelandview object MV, and then calls:

This.processdispatchresult (processedrequest, Response, Mappedhandler, MV, (Exception) dispatchexception);

The main invocation of this method is render (MV, request, response), which checks what type of view is, and, if it is a string type, converts the Resolveviewname method of the class to the view type through the Viewresolver interface. , in short, to get the view instance object, call the view interface implementation class Render method, to achieve page rendering:

Public interface View {    String response_status_attribute = View.class.getName () + ". ResponseStatus";    String path_variables = View.class.getName () + ". Pathvariables";    String Selected_content_type = View.class.getName () + ". Selectedcontenttype";    String getContentType ();        @param model MAP with name Strings as keys and corresponding model    void render (map<string,?> model, Httpserv Letrequest request, HttpServletResponse response) throws Exception;}

The common view implementation classes are:

    • Internalresourceview
    • Jstlview--Jstl file
    • Freemarkerview-FTL file (what is Freemarker?)
    • Wait a minute

If you want to see a view rendering of the specific process can refer to the "Internalresourceview source analysis (to write)"

PS: Different views correspond to different view resolvers, such as Freemarkerview need freemarkerviewresolver to complete the parsing.

9. Initflashmapmanager Redirection Property Store

The official document states: Flashmapmanager is used to retrieve and save Flashmap instances of the policy interface

What is that flashmap?

Look at the Official document description:

/** * A flashmap provides aFor oneRequestTo store attributes intendedfor * useIn another. Thisis most commonly needed if redirecting from one URL *to another-e.g. the post/redirect/Get pattern. A Flashmapis saved before * the redirect (typicallyIn the session)andis made available after the * redirectand removed immediately. * * <p>a Flashmap can beSet upWith aRequest PathandRequest parametersto * help identify the targetRequest. Without this information, a Flashmapis * made availableto theNextRequest, which mayor MayNot being the intended * recipient.On a redirect, the target URLis knownand a flashmap can be * updated with that information. This is did automatically when the * {@code Org.springframework.web.servlet.view.RedirectView} is used. * * <p>note:annotated Controllers would usually not use Flashmap directly. * See {@code org.springframework.web.servlet.mvc.support.RedirectAttributes} * for an overview of Using Flash attributes in Annotated controllers. * * @author Rossen Stoyanchev * @since 3.1 * @see Flashmapmanager * * @SuppressWarnings ( "serial") public final class Flashmap extends hashmap< string, object> implements comparable<flashmap> {...}      

Simple translation:

Flashmap provides a feature for a request to store properties that are used in another request. This is most often required when one URL is redirected to another URL-for example, Post/redirect/get mode. Flashmap is saved (usually in the session) before redirection and is available and immediately deleted after redirection.

You can use the request path and request parameters to set Flashmap to help identify the target request. Without this information, FLASHMAP will be available for the next request (which may or may not be expected). In a redirect, the target URL is known and can be used to update the FLASHMAP, which is done automatically by Org.springframework.web.servlet.view.RedirectView.

Note: A commented controller typically does not use Flashmap directly. For an overview of using Flash properties in a commented controller, see org.springframework.web.servlet.mvc.support.RedirectAttributes.

The above description shows that Flashmap and Flashmapmanager are primarily used for redirecting data preservation. Redirection will have to mention that when Redirectview,redirectview jumps, the parameters in the request before the jump are saved to flashmap and then saved through Flashmanager:

Get the data that the original request carries  flashmap Flashmap = requestcontextutils.getoutputflashmap (request);//Save the data as the data requested after the jump is used  Flashmapmanager.saveoutputflashmap (Flashmap, request, response);

When the redirected request is redirected in the browser, it will be intercepted again by Dispathcerservlet, in the Dispatcherservlet Doservice method, One step is to get the values in the saved Flashmap from the Flashmanager:

Flashmap Inputflashmap = this.flashMapManager.retrieveAndUpdate (request, response);

This redirects the request to use some of the previous property values.

Summarize:

Flashmap is simply a hashmap for data preservation, primarily for redirection, SPRINGMVC the default flashmapmanager is Sessionflashmapmanager

Org.springframework.web.servlet.flashmapmanager=org.springframework.web.servlet.support.sessionflashmapmanager

(End of full text)

Springmvc Dispatchservlet Initialize nine load strategies (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.