Tuesday, 21 June 2011

Accessing Spring Resource Bundles from your JSPs

Yesterday’s blog demonstrated how to create and load a set of multi-lingual property file that you can use to internationalise your application. Today’s short blog expands this idea and demonstrates how to access property file values from within your web-apps JSP pages.

The usual way to achieve this is to use JSP tag-libraries. There are many flavours of tag-lib around that duplicate much of the same functionality. My preferred flavours are: struts, JSTL and the Spring tag-libs and it’s the Spring tag lib that I’m using to read property file values.

The first step in using a tag-lib is to add an import line, usually at the top of your JSP:

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

From then on it’s just a matter of using spring:message tag to display properties:

<spring:message code="label.endDate"/>

...where label.endDate is either:

label.endDate=End Date:

...in the UK file or:

label.endDate=Date de fin:

...in the French property file.

Just to demonstrate that this works, below are a couple of snippets from a search screen; one in English and one in French. They’re both the same screen, in displaying them, the only change I made was to change the Locale information from UK to France in my Window’s Control Panel.

In English:

In French:

Changing the Locale via Windows Control Panel works with IE, but may not with other web-browsers (it doesn't work with Chrome).

Just to complete the picture, below is a sample of the JSP that creates the screen extracts above:

<div class="field">
 <%-- spring:message - this will read stuff from the Resource Bundle
 label for : represents the input type this is connected to  
 <label for="eventType"><spring:message code="label.eventType"/></label>
 'path' is the name of the searchCriteria javabean attribute: the getter(...) method
 <form:input path="eventType" id="eventType" cssClass="text" maxlength="17" title="Search the Event Type" accesskey="E" tabindex="1"/>
<div class="field">
 <label for="description"><spring:message code="label.description"/></label> 
 <form:input path="description" id="description" cssClass="text" maxLength="18" title="Search on all or part of the event description" accesskey="S" tabindex="2"/> 
<div class="field">
 <label for="startDay"><spring:message code="label.startDate"/></label> 
 <form:input path="startDay" id="startDay" size="2" cssClass="text" maxLength="2" title="The start day" accesskey="D" tabindex="3"/>
 <spring:message code="label.slash"/>
 <form:input path="startMonth" id="startMonth" size="2" cssClass="text" maxLength="2" title="The start month" accesskey="M" tabindex="4"/>
 <spring:message code="label.slash"/>
 <form:input path="startYear" id="startYear" size="4" cssClass="text" maxLength="4" title="The start year" accesskey="Y" tabindex="5"/> 
<div class="field">
 <label for="endDay"><spring:message code="label.endDate"/></label> 
 <form:input path="endDay" id="endDay" size="2" cssClass="text" maxLength="2" title="The end day" tabindex="6"/> 
 <spring:message code="label.slash"/>
 <form:input path="endMonth" id="endMonth" size="2" cssClass="text" maxLength="2" title="The end month" tabindex="7"/> 
 <spring:message code="label.slash"/> 
 <form:input path="endYear" id="endYear" size="4" cssClass="text" maxLength="4" title="The end year" tabindex="8"/> 

No comments: