Urlbasedviewresolver and Internalresourceviewresolver of Spring MVC

Source: Internet
Author: User

Spring MVC uses Viewresolver to associate to a specific view object based on the view name returned in the controller. Use the View object to render the return value to produce the final view, such as Html,json or PDF.

In spring MVC, the Viewresolver object is registered through the Viewresolver Bean object configured in the servlet context configuration file, which is in spring The process of using MVC in Dispatcherservlet is similar to the handlermapping and Handeradapter mentioned earlier.

Dispatcherservlet initialization, the Viewresolver object list is generated first based on the bean list of the Viewresolver interface implemented in the beans registered in the current context. and sorted according to the order attribute in these viewresolver, this viewresolver has the lowest priority if the Order property is not configured.

When Handleradapter is finished calling handler method to return to the View object, Dispatcherservlet calls the Resolveviewname methods in the order of the Viewresolver list that is currently registered To find the actual corresponding View object based on ViewName and generate the final user view based on the Render method in the first returned view object.

Similar to handlermapping and Handeradapter, Dispatcherservlet also provides parameters to allow the user to specify whether to load only their own viewresolver, When the property detectallviewresolvers is set to false, spring MVC looks for the bean named Viewresolver as the current unique viewresolver.

        <init-param>
<param-name>detectAllViewResolvers</param-name>
<param-value>false</param-value>
</init-param>

Urlbasedviewresolver and Internalresourceviewresolver

In the previous example, it was defined in the servlet context configuration file to map the returned viewname to the corresponding JSP.

    <bean id= "Viewresolver"
class= "Org.springframework.web.servlet.view.UrlBasedViewResolver" >
<property name= "Viewclass"
Value= "Org.springframework.web.servlet.view.JstlView"/>
<property name= "prefix" value= "/web-inf/jsp/"/>
<property name= "suffix" value= ". jsp"/>
</bean>

The implication of this definition is that Urlbasedviewresolver will use the Jstlview object to render the result, and add the handler method back to the Modelandview base, plus the directory prefix/web-inf/jsp/ and the file name suffix. jsp. For example, if the result returns a viewname of HelloWorld, the corresponding actual JSP is/web-inf/jsp/helloworld.jsp

When the returned viewname is prefixed with forward:, spring MVC transfers The result forward to the corresponding view, such as Forward:helloworld. This is also the default usage pattern for spring MVC.

When the returned viewname is prefixed with redirect:, spring MVC transfers the result to the corresponding view by redirect way. such as Redirect:helloworld

Internalresourceviewresolver is a subclass of Urlbasedviewresolver, which takes Internalresourceview as the default view class, If there is a jstl jar package in the current classpath, the result is rendered using Jstlview as the default view . The following definitions of using internalresourceviewresolver should therefore be the same as those previously defined using Urlbasedviewresolver. Viewresolver.

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

In addition to this, Internalresourceviewresolver also offers
Alwaysinclude property to require that the returned results be used in the include method instead of forward
The Exposecontextbeansasattributes property is used to expose beans in the current spring environment to the page as a request attritbutes.
The Exposedcontextbeannames property to limit the list of names of spring beans that can be exposed to the page.

Urlbasedviewresolver and Internalresourceviewresolver of Spring MVC

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.