SpringMVC4 (13) View parser classification

Source: Internet
Author: User

In the article "SpringMVC4 (7) Model View method source Synthesis Analysis", we introduced the use of Modelandview, which will be returned as a return value after the control layer method call, which encapsulates our business logic data and view object or view name
。 Next, the view object tends to render further to the model, which is further parsed by the view parser and responds to the front end. Below, we detail the various classifications of view and view parsers.
In the view interface, a core method is defined:

voidthrows Exception;

Its role is primarily to render model data, integrate Web resources, and respond to customers in a specific form, either as complex JSP pages or as simple JSON, XML strings.
For different response forms, spring has designed different view implementation classes for us:

For different view objects, we use different view parsers to do the instantiation work. We can configure multiple view parsers in the spring context and specify the resolution precedence order between them through the Order property, the smaller the order, the higher the resolution view of the corresponding viewresolver will be. When a viewresolver returns a view object that is null when it is parsed, it means that the viewresolver cannot parse the view, at which point it is given a lower priority to parse until the parsing is complete, and if all the view parsers are unable to complete the parsing, The exception is thrown.
Similar to the view, spring also provides us with a number of view parser implementation classes:

1. Abstractcachingviewresolver

This is an abstract class, this view parser will save its once parsed view, and then each time to parse the view from the cache, if the corresponding view is found directly back, if not a new view object is created, and then put it in a cache for the map, The new view is then returned. Using this view caching method minimizes the performance problems of the resolution view.

2. Urlbasedviewresolver

It inherits the Abstractcachingviewresolver , prefix, suffix** splicing url** way to parse the view, support the returned view name contains redirect:, Forward, such as prefixes, are redirected or forwarded. When using Urlbasedviewresolver, you must specify the attribute Viewclass, which is the type of view to parse into, and the most commonly used is Internalresourceview, which uses it to show JSP, but when we use JSTL We must use the Jstlview. Here is an example definition of a urlbasedviewresolver:

<Bean class="Org.springframework.web.servlet.view.UrlBasedViewResolver">    < property name="prefix" value="/web-inf/" />   < property name="suffix" value=". jsp" />   < property name= "viewclass" value=" Org.springframework.web.servlet.view.InternalResourceView "/></Bean>
3. Internalresourceviewresolver

It is a subclass of Urlbasedviewresolver, so the features supported by Urlbasedviewresolver are supported. Internalresourceviewresolver will parse the returned view name into the Internalresourceview object, Internalresourceview will put the Controller The model properties returned by the processor method are stored in the corresponding request property, and the request ForWord is redirected to the destination URL via RequestDispatcher on the server side. Here is a configuration instance:

< Bean  class  = " Org.springframework.web.servlet.view.InternalResourceViewResolver ";  property  name  = "prefix"  value  = "/web-inf/" />  <property  name
      = "suffix"  value  = ;  </property ;  </bean ;   

If we need to use jstlview, we need to specify that the Vlewclass property is Jstlview.
In the previous test instance, we have been using this view parser, so no longer an example.

4. Beannameviewresolver

According to its name, the ID of the view that we registered as Bean,bean in the spring container is the view name. When we return to the view in the control layer, Beannameviewresolver will automatically find the corresponding view bean based on our attempted name, and we'll look at an example:

1. Configuring the View resolver and view
<Bean class="Org.springframework.web.servlet.view.BeanNameViewResolver">    < property name="Order" value="1"/><!--set the highest priority, start with parsing first--</Bean><bean id= "Hello" class=" Org.springframework.web.servlet.view.InternalResourceView ">   < property name="url" value="/web-inf/views/hello.jsp"/ ><!--access the corresponding JSP file--</Bean>
2. Configuring the Controller

Once you have configured the view and view resolver, we can access the view in the control layer through the following methods:

@Controllerpublicclass ViewController {    @RequestMapping("view1")    publicview1(){        return"hello";//返回字符串直接为视图名称,解析器会找到名称对应的视图Bean解析视图    }}

We will jump to the corresponding hello.jsp view when we visit View1 in the browser.

3. Xmlviewresolver

It inherits from the Abstractcachingviewresolver abstract class, so it is also supported for view caching. Xmlviewresolver needs to be given an XML configuration file to define the Bean object for the view. The Bean object for each view defined in the file is given a name, and then Xmlviewresolver will look for the view bean that corresponds to the name in the specified configuration file according to the logical view name returned by the Controller processor method. Used to process the view. The configuration file defaults to the/web-inf/views.xml file, which can be specified in the Location property of the Xmlviewresolver if the default value is not used.
It needs to be clear that using xmlviewresolver ultimately does not necessarily need to output an XML view
The following is an example of using Xmlviewresolver:

1. Add the Xmlviewresolver bean definition to the SPRINGMVC configuration file.

Use the Location property to specify where its configuration file is located, and the Order property specifies the priority of its processing view when there are multiple viewresolver.

<bean class="org.springframework.web.servlet.view.XmlViewResolver">   <property name="location" value="/WEB-INF/views.xml"/>   <property name="order" value="1"/></bean>
2. Configure the desired view definition in the xmlviewresolver corresponding configuration file.

In the following code we configure a internalresourceview named Hello with the URL property "/index.jsp".

<bean id="hello" class="org.springframework.web.servlet.view.InternalResourceView">   <property name="url" value="/WEB-UBF/view/hello.jsp"/></bean>
3. Configuring the Web Layer Controller
@Controllerpublicclass ViewController {    @RequestMapping("view2")    publicview2(){        return"hello";//返回字符串直接为视图名称,解析器会找到名称对应的视图Bean解析视图    }}

Similar to the Beannameviewresolver instance, we visit View2,xmlviewresolver to find the corresponding ID in the/web-inf/views.xml view to complete the parsing.

5. Resourcebundleviewresolver

It inherits from Abstractcachingviewresolver, but it does not cache views. As with Xmlviewresolver It also requires a configuration file to Defines the relationship between the logical view name and the True View object , unlike the Resourcebundleviewresolver configuration file is a property file and must be placed in the Classpath path below, by default this configuration file is in the * classpath root directory views.properties file , if you do not use the default value, you can through the property BaseName or Basenames to specify * . BaseName just specifies a base name, Spring will look for a property file with the specified BaseName starting at the specified classpath root for View resolution, as specified BaseName is base, then base.properties , Baseabc.properties, and so on, the properties file starting with base will be used by Spring as the resource file for the Resourcebundleviewresolver resolution view. The contents of the property profile used by Resourcebundleviewresolver are similar to this:

resourceBundle.(class)=org.springframework.web.servlet.view.InternalResourceViewresourceBundle.url=/index.jsptest.(class)=org.springframework.web.servlet.view.InternalResourceViewtest.url=/test.jsp

Corresponding to the above configuration, spring will parse it into the following bean definition:

<bean id= "resourcebundle" class=" Org.springframework.web.servlet.view.InternalResourceView ">   < property name="url" value="/index.jsp"/></Bean><bean id="Test" class=" Org.springframework.web.servlet.view.InternalResourceView ">   < property name="url" value="/test.jsp"/></Bean>

Next you'll talk about Spring's rules for generating beans through the properties file. It will press the property name defined in the properties file to the last point. "To split the front of the point as the bean name , the content behind the point as the Bean's properties ." There are several special attributes that Spring uses to enclose them in parentheses, which are typically the corresponding attribute, but not all attribute of the Bean object can be used this way. where (Class) is a, in addition to (class), there is (scope), (parent), (abstract), (Lazy-init). In addition to these special properties, Spring will treat them as the generic properties of the Bean object, which is the property of the Bean object. Therefore, the following two Bean objects will be generated based on the above property configuration file:

From the configuration file used by Resourcebundleviewresolver we can see that it can parse many different types of view as well as xmlviewresolver, because their view is specified by configuration, which means we can specify a View is Internalresourceview, B view is Jstlview.

In addition to the above view parser, the following two template view parsers are commonly used: Freemarkerviewresolver, Volocityviewresolver, in a later article will be specifically mentioned.

Reference book: "Spring 3.x Enterprise application Development Practical"
Reference article: http://www.tuicool.com/articles/RVb67r

SpringMVC4 (13) View parser classification

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.