Spring MVC Viewresolver Technology

Source: Internet
Author: User
Tags locale

In spring MVC, when the controller puts the request processing results into Modelandview, Dispatcherservlet chooses the appropriate view to render according to Modelandview.

    1. How to choose the right view?
    2. How is the View object created?

The Viewresolver interface defines the Resolverviewname method, creating the appropriate type of view implementation based on ViewName. What about configuration Viewresolver? In spring, Viewresolver is present as a spring bean and can be configured in a spring configuration file, such as the following code, which configures JSP-related viewresolver.

    <!--resolves view names to protected. JSP resources within The/web-inf/views directory -          <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">              < Propertyname= "prefix"value= "/web-inf/views/"/>              < Propertyname= "suffix"value= ". jsp"/>          </Bean>  

To let Dispacherservlet load the default viewresolver, if not set viewresolver,spring use Internalresourceviewresolver for parsing.

Spring implements Viewresolver non-abstract classes and we often use the following four types of Viewresolver:

Internalresourceviewresolver Resolves a logical view name to a path
Beannameviewresolver Resolves the logical view name to the Bean's Name property, thus finding the bean that defines the view based on the Name property
Resourcebundleresolver As with Beannameviewresolver, only the defined View-bean are in a properties file, and the properties file is loaded with this class
Xmlviewresolver Just like Resourcebundleresolver, the definition of View-bean in an XML file, using this class to load an XML file

Using the Multi-view parser: You can define multiple viewresolver in [spring-dispatcher-name]-servlet.xml:

<bean id=class="Org.springframework.web.servlet.view.InternalResourceViewResolver" >< Property name="prefix" value="/jsp/"/><property name="suffix" value=". JSP"/></bean>
Class= "... Beannameviewresolver "><property name=" order "value=" 1 "></property></bean>
Class= "... Xmlviewresolver "><property name=" order "value=" 0 "></property></bean>

Dispatcherservlet loads all the viewresolver into a list and resolves it by priority. Note the lower the value in order, the higher the priority. And the ID is viewresolver

The priority of the Viewresolver is the lowest.

Viewresolver is configured with beans, so it is very easy to scale up, customize the Viewresolver to suit your needs, and configure it in the configuration file.

The Viewresolver interface declares the Resolverviewname method, and the main function of this method is to create the appropriate type of view object based on the ViewName information given in the Modelandview, combined with the associated configuration.

The Viewresolver interface is called in Dispatcherservlet, and when Dispatcherservlet has finished calling the controller, it gets a Modelandview object, Then Dispatcherservlet calls the Render method for view rendering.

    protected voidRender (Modelandview MV, httpservletrequest request, httpservletresponse response)throwsException {//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 NewServletexception ("Could not resolve view with name '" + mv.getviewname () + "' on Servlet with name '" +Getservletname ()+ "'"); }              }              Else {                  //No need to Lookup:the Modelandview object contains the actual View object. View =Mv.getview (); if(View = =NULL) {                      Throw NewServletexception ("Modelandview [" + MV + "] Neither contains a view name nor a" + "view Obje CT in servlets with name ' "+ getservletname () +" ' "); }              }                    //Delegate to the View object for rendering.             if(logger.isdebugenabled ()) {Logger.debug ("Rendering View [" + View + "] in Dispatcherservlet with Name '" + getservletname () + "'");          } view.render (Mv.getmodelinternal (), request, response); }  
In the Dispatcherservlet class, the Init method has been initialized, and the configured viewresolver information is stored in the viewresolvers. Call the Resolverviewname method in the Render method and call Viewresolver in this method to get the View object.
    protectedView Resolveviewname (String viewName, map<string, object>model, locale locale, httpservletrequest request)throwsException { for(Viewresolver viewresolver: This. Viewresolvers) {View View=viewresolver.resolveviewname (viewName, locale); if(View! =NULL) {                  returnview; }          }          return NULL; }  

The choice here for Viewresolver is done through loops, selecting only the first one that meets the requirements, so when defining viewresolver, you need to be careful about defining its precedence.

Let's focus on the class structure of viewresolver.

With regard to the creation of the View object, the different Viewresolver solutions are the same for each department. If Beannameviewresolver is the bean that chooses the name according to ViewName (note that the bean's scope needs to be thread-safe), Urlbasedviewresolver uses a reflection mechanism, based on The Viewclass information creates a view object, so this view is not managed by the IOC container. You can nest Viewresolver in Contentnegotiationviewresolver, and choose the appropriate viewresolver based on the different request types.

After Dispatcherservlet gets the view object, it calls the view's Render method to perform the actual rendering work.

Finally, take a look at the class structure diagram of the view.

With the view class structure shown above, spring has provided us with a range of available view. Also, if the currently provided view does not meet our requirements, it can be extended by implementing the View interface. If you need to use the Jfreechart drawing based on the data in the model, or when you download the data as a file, we can extend the Jfreechartview and Filedownloadview and so on, so that the same data can be displayed in different ways more flexibly.

Spring MVC Viewresolver Technology

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.