Home

This Chapter
-Chapter 11: Message Handling and Internationalization
-Working with Locales in Struts
-Registering Properties Files
-The HTML Tag Librarys’s errors Tag
-The HTML Librarys’s messages Tag
-JSTL I18N Library
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

JSTL I18N Library

JSTL provides the following tags for displaying and formatting internationalized messages: setLocale, bundle, setBundle, message, param, and requestEncoding. Each of these tags is explained in a subsection below. See also Chapter 7 on JSTL in general.

setLocale

The setLocale tag is used to set the locale for the scope specified in the scope attribute of this tag. For example, if the scope is session, the specified locale applies to all JSPs in the same user session. If no locale is explicitly specified using setLocale, the locale found in the user request will be used.

The locale set using setLocale is used by the bundle and setBundle tags to select a ResourceBundle. The setLocale tag persists the locale by storing a Locale object referenced by the scoped variable javax.servlet.jsp.jstl.fmt.locale in the specified scope. The bundle and setBundle tags look for a scoped variable having the same name to determine the current locale. If no javax.servlet.jsp.jstl.fmt.locale scoped variable is found, the user's locale is used.

The syntax of setLocale is as follows:

<fmt:setLocale value="locale"
  [variant="variant"]
  [scope="{page|request|session|application}"]/>

The attributes for <fmt:setLocale> are described in Table 11.3.

Attribute Type Description
value java.lang.String or java.util.Locale A string representation of a locale or a locale. If a String is used, it must be a two-letter ISO 639 language code, optionally followed by an underscore and an ISO 3166 country code.
variant String vendor- or browser-specific variant.
scope String The scope of the locale configuration variable.

Table 11.3: The setLocale tag’s attributes

For example, the following setLocale tag sets the locale to English (United States) for the current session:

<fmt:setLocale value="en_US" scope="session"/>

bundle

The bundle tag loads a ResourceBundle based on the current locale. The ResourceBundle can then be used from inside this tag’s body.

The bundle tag has the following syntax:

<fmt:bundle basename="basename" [prefix="prefix"]>
  body content
</fmt:bundle>

The attributes for the bundle tag are listed in Table 11.4.

Attribute Type Description
basename String The base name for the ResourceBundle
prefix String The prefix to be prepended to the value of the message key of any nested message tag.

Table 11.4: The bundle tag’s attributes

The following is an example of the bundle tag, used with a message tag as its body content:

<fmt:bundle basename="AppResources">
  <fmt:message key="farewell"/>
</fmt:bundle>

setBundle

setBundle is used to load a ResourceBundle and is similar to bundle. However, setBundle applies to all JSPs in the scope specified in the scope attribute of this tag. A message tag does not have to reside in its body content to use the ResourceBundle it loaded.

The following is the syntax of the setBundle tag:

<fmt:setBundle basename="basename"
  [var="varName"]
  [scope="{page|request|session|application}"]/>

The attributes for setBundle are listed in Table 11.5.

Attribute Type Description
basename String The base name for the ResourceBundle
var String The name of the scoped variable that can be referenced in a message tag to use the ResourceBundle loaded by this tag.
scope String The scope for var

Table 11.5: The setBundle tag’s attributes

Here is an example of how to use setBundle:

<fmt:setBundle basename="AppResources"/>
<fmt:message key="welcome"/>

message

The message tag is used to read a message from the specified or current ResourceBundle and display the message. Its syntax has three forms. The first form is used without body content:

<fmt:message key="messageKey"
  [bundle="resourceBundle"]
  [var="varName"]
  [scope="{page|request|session|application}"]/>

The second form is used with a body to specify message parameters:

<fmt:message key="messageKey"
  [bundle="resourceBundle"]
  [var="varName"]
  [scope="{page|request|session|application}"]>
  <fmt:param> subtags
</fmt:message>

The third form is used with a body to specify key and optional message parameters:

<fmt:message [bundle="resourceBundle"]
  [var="varName"]
  [scope="{page|request|session|application}"]>
  key
  optional <fmt:param> subtags
</fmt:message>

The list of attributes is given in Table 11.6.

Attribute Type Description
key String The key of the message in the ResourceBundle
bundle LocalizationContext The name of the scoped variable that can be referenced in a message tag to use the ResourceBundle loaded by this tag.
var String The name of the scoped variable to store the obtained localized message
scope String The scope for var.

Table 11.6: The message tag’s attributes

Here is an example of the message tag:

<fmt:setBundle basename="AppResources"/>
<fmt:message key="welcome"/>

param

The param tag is used to pass a parameter to the enclosing message tag. Its syntax has two forms. The first form specifies the value via the value attribute:

<fmt:param value="messageParameter"/>

The second form specifies the value through the body content:

<fmt:param>
  body content
</fmt:param>

The value attribute is the argument used for the replacement of a part in a localized message. It can be assigned a dynamic value of type java.lang.Object.

As an example, here is a param tag:

<fmt:message key="welcome">
  <fmt:param value=”time”/>
</fmt:message>

requestEncoding

This tag is used to set the encoding of the current HttpServletRequest object. Here is its syntax:

<fmt:requestEncoding [value=”charsetName”]/>

The value attribute is the name of character encoding to be applied when decoding request parameters. It can be assigned a dynamic value of type String.

Previous
Next