Wednesday, July 6, 2011

SPRING - Using Resource Bundle to write on Web Pages (JSP)

To achieve internationalization, many websites prefer keeping their data in the properties file and display them in the web page. With this approach, the web site gets the option to select the desired language in which the contents has to get displayed.

In Spring this can be achieved easily by using the following steps.
  • Add this entry to Spring context file.
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="Messages" /> bean>

The 'Messages' refers to the name of the properties file directly under the source folder.
  • Add the following entry to your properties file Messages.properties
welcomeMsg=Hi, This message is from the Messages.properties file
  • You can read this value in the JSP page.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<fmt:message key="welcomeMsg">fmt:message>

No comments:

Post a Comment