Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring resourcebundleviewresolver

 
 

Share on Google+Share on Google+

resourcebundleviewresolver

Advertisement
resourcebundleviewresolver

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"%>

<html>

<head>

<title>Resource Bundle View Resolver</title>

</head>

<body>

<center>

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

</center>

</body>

</html>

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="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"

version="2.5">

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

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

</servlet-mapping>

<welcome-file-list>

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

</welcome-file-list>

<taglib>

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

<taglib-location>/WEB-INF/spring.tld</taglib-location>

</taglib>

</web-app>

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"

class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

<property name="basename">

<value>spring-views</value>

</property>

</bean>

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 spring-views.properties file is

resourcebundle.(class)=org.springframework.web.servlet.view.JstlView

resourcebundle.url=/WEB-INF/jsp/resourcebundle.jsp

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

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

<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-2.5.xsd"

xmlns:p="http://www.springframework.org/schema/p">

<bean id="viewResolver"

class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

<property name="basename">

<value>spring-views</value>

</property>

</bean>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/resourcebundle.html">

<ref bean="resourcebundleController"/>

</entry>

</map>

</property>

</bean>

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

</bean>

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

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

</bean>

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

</beans>

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:

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

pageEncoding="ISO-8859-1"%>

<html>

<head>

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

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

</head>

<body>

<center>

<table>

<tr>

<td>

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

</td>

</tr>

</table>

</center>

</body>

</html>

Step 5:

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

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 {  
@Override
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

Download this example code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 2, 2011

Related Tutorials

 

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com