0
Sponsored Links


Ad by Google
We have implemented step by step Spring MVC Hello World Application here, and few basics or you can say initial setup are required to create Spring MVC application like Front-Controller and ViewResolver. So here we are going to talk about What is View Resolver in Spring MVC? and What are the View Resolvers with simple example.

Well View Resolvers are used to populate your models on the browser, or you can say resolve your viewing components by mapping their view names. Spring MVC provides an interface called ViewResolver to map your logical view names with actual viewing components like jsp,jstl,pdf,excel etc.

There are three different implementation of ViewResolver to resolve logical views to actual views and those are listed below, and we also see an example of each of them.
  1. InternalResourceViewResolver
  2. ResourceBundleViewResolver
  3. XmlViewResolver
We will see an example of all three view resolvers, and of course, we will return an excel file instead of jsp in one of our example using ResourceBundleViewResolver.

What is InternalResourceViewResolver ?

The InternalResourceViewResolver is an implementation of ViewResolver interface and also extends UrlBasedViewResolver class. The InternalResourceViewResolver will maps the jsp, servlet and jstl. It uses prefix and suffix to prepare the final view page url, configured in *-servlet.xml file.

How to use InternalResourceViewResolver:
Place the below entry inside *-servlet.xml file.

<bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
Now all the views name return from your controller class will maps inside WEB-INF/pages with suffix as .jsp. It's good practice to put all your view component inside WEB-INF folder for security purpose. Putting viewing components inside WEB-INF folder will hide them to direct access from the URL, and allow only controller to access the viewing components. From here you can see the complete example using Maven and also download the complete sample project developed in Eclipse IDE.

What is ResourceBundleViewResolver ?

The ResourceBundleViewResolver is an implementation of ViewResolver interface, it uses bean definition from ResourceBundle specified by the bundle basename. The bundle is defined in a properties file, default location is in class path and the default bundle basename is views.properties. The ResourceBundleViewResolver will also helps you to achieved Internationalization and also sometimes you want to return excel/pdf file as a view instead of jsp,jstl, that can achieved by using ResourceBundleViewResolver.

How to use ResourceBundleViewResolver :
Create a views.properties file in your class path with below entries to return views as excel file instead of jsp,jstl.
excel.(class)=com.javamakeuse.springmvc.util.ExcelBuilderForResourceBundleViewResolver
Now add below entry in your *-servlet.xml file
<bean id="viewResolver"
  class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
 <property name="basename" value="views" />
</bean>
From here you can see the step by step example of ResourceBundleViewResolver implementation and also you can download the sample application.

What is XmlViewResolver?

The XmlViewResolver is also an implementation of ViewResolver interface and uses bean definitions from the dedicated xml file, the default name of the XML file is views.xml and default location of the views.xml file is in class path. You can map your view name with views inside views.xml file, but XmlViewResolver will not support internationalization.

How to use XmlViewResolver:
Create views.xml file inside class path with below mapping entries.
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="home"
        class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/pages/xml-home.jsp" />
    </bean>
</beans>

Now add below entry into your *-servlet.xml file
<bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
  <property name="location">
 <value>/WEB-INF/views.xml</value>
  </property>
</bean>

For complete step by step example of XmlViewResolver implementation you can visit here.
Sponsored Links

0 comments:

Post a Comment