The ViewResolver interface

The logical view name and other details are returned by the user-defined controller to the Front Controller. The view name is of type String, which needs to be resolved by ViewResolver.

The following are a few ViewResolvers that can be used to render the view:

  • XmlViewResolver: This helps in viewing the file written in XML. It uses the default configuration from WEB-INF/views.xml, which contains the view beans having the same DTD as that of the Spring beans configuration file. The configuration can be written as follows:
<bean id="myHome" 
  class="org.springframework.web.servlet.view.JstlView"> 
  <property name="url" value="WEB-INF/jsp/home.jsp"/> 
<bean> 

The logical view name myHome is mapped to the actual view WEB-INF/jsp/home.jsp.

One bean can also be referred to the view mapped for some other bean, as follows:

<bean id="logout" 
  class="org.springframework.web.servlet.view.RenderView"> 
  <property name="url" value="myHome"/> 
<bean> 

The logout bean is not mapped for any actual view file, but it uses the myHome bean to give the actual view file.

  • UrlBasedViewResolver: This gives the direct mapping of the URLs to the logical view name. It is preferred where the logical names match the view resource. It has a prefix and a suffix, as its properties help in getting the actual view names with its location. The class is unable to resolve the views that are based on the current locale. The following configuration can be written to enable URLBasedViewResolver:
<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 JstlView is used to render the view page. The page name and location in our case is prefix+ view_name_from_controller+suffix.

  • InternalResourceViewResolver: This is a subclass of UrlBasedViewResolver used to resolve the internal resource, which can serve as views using properties such as prefix and suffix similar to its parent class. AlwaysInclude, ExposeContextBeansAsAttributes, and ExposedContextBeanNames are a few extra properties of the class, adding the advantage of using it more frequently than its parent class. The following configuration is similar to the way we configured UrlBasedViewResolver in the previous example:
<bean id="viewResolver" class=            
"org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value= "org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <bean>

It can verify the existence of the page only when it lands on it, and not before that.

  • ResourceBundleViewResolver: This uses the definition from ResourceBundle specified in the configuration. The default file is used to define the configuration as views.properties. The configuration is as follows:
<bean id="viewResolver" class=      
  "org.springframework.web.servlet.view.ResourceViewResolver"> 
  <property name="base" value="web_view"/> 
</bean> 

The view.properties configuration will specify the details of the View class to be used and the URL mapping to the actual view, which is as follows:

home.(class)= org.springframework.wev.servlet.view.JstlView 

The following line states the mapping of the view named homepage:

homepage.url= /WEB-INF/page/welcome.jsp 
  • TilesViewResolver: The Tiles framework is used to define the templates which define the layout of the pages. The defined templates will be reused to keep a consistent look and feel of the application. The page definitions are defined in the tiles.def file as tile, header, footer, and menus, which are assembled in the page as runtime. The logical name returned by the controller matches the name of the tiles template that is rendered by the view resolver.

Apart from these view resolvers, Spring also has FreeMarkerViewResolver, TileViewResolver, VelocityLayoutViewResolver, VelocityViewResolver, and XsltViewResolver.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.214