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"
  <property name="prefix">
  <property name="suffix">
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.
Now add below entry in your *-servlet.xml file
<bean id="viewResolver"
 <property name="basename" value="views" />
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"

    <bean id="home"
        <property name="url" value="/WEB-INF/pages/xml-home.jsp" />

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

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


Post a Comment