ResourceBundleViewResolver Example


ResourceBundleViewResolver example in Spring 2.5 MVC

Learn how to use ResourceBundleViewResolver in Spring 2.5 MVC.

The ResourceBundleViewResolver implements ViewResolver interface and uses the bean definitions in the properties files. In this example we will discuss how to used ResourceBundleViewResolver and how to create a properties files for the ResourceBundleViewResolve.

Step 1:

Now we will create a index.jsp that have a hyperlink for user click. The code of the index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>



<title>Resource Bundle View Resolver</title>




<a href="resourcebundle.html">Resource Bundle View Resolver</a>




Step 2:

Now we will customize the the web.xml file for the DispatcherServlet. The code of the web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi=""









</servlet> 0



<url-pattern>*.html</url-pattern> 1



<welcome-file>index.jsp</welcome-file> 2



<taglib-uri>/spring</taglib-uri> 3



</web-app> 4

Step 3:

Now we will create a dispatcher-servlet.xml file in the /WEB-INF/ folder that is the main configuration file for this project. The dispatcher-servlet.xml file contain configuration code for the ResourceBundleViewResolver like:

<bean id="viewResolver" 5


<property name="basename">

<value>spring-views</value> 6



In this code, we have create a bean for ResourceBundleViewResolver  and set id for this bean. This bean property that's name basename and have value spring-views this is the name of the properties file name. The properties file contain the class and url properties. The code of the file is 7



The full code of the dispatcher-servlet.xml is: 8

<?xml version="1.0" encoding="UTF-8"?>


xmlns="" 9


xsi:schemaLocation=" http:

xmlns:p=""> 0

<bean id="viewResolver"


<property name="basename"> 1



</bean> 2

<bean id="urlMapping"


<property name="interceptors"> 3


<ref local="localeChangeInterceptor"/>

</list> 4


<property name="urlMap">

<map> 5

<entry key="/resourcebundle.html">

<ref bean="resourcebundleController"/>

</entry> 6



</bean> 7

<bean id="resourcebundleController" class="net.roseindia.web.ResourceBundleController">


<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 8

<property name="paramName" value="hl"/>


<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> 9


Step 4:

Now we will create a resourcebundle.jsp in the /WEB-INF/jsp/ folder. This file display message for the user for successfully used ResourceBundleViewResolver. The code of the resourcebundle.jsp is: 0

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


<html> 1


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Resource Bundle View Resolver Example</title> 2



<center> 3



<td> 4

<b>This is Resource Bundle View Resolver Example.</b>


</tr> 5



</body> 6


Step 5:

Now we will create class in the project src folder to control the user request and implement AbstractController and return the ModelAndView. The code for the is: 7

package net.roseindia.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class ResourceBundleController extends AbstractController {  
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse responsethrows Exception {
String Mess = "Resource Bumdle Example With Controller";
System.out.println("------>" + Mess);
ModelAndView modelAndView = new ModelAndView("resourcebundle");  
return modelAndView;

Step 6:

Now we will run this application and see the output like:


Now click on this link and see like:

Download code 9

Download this example code