background image

Formatting Tags

<< Messaging Tags | SQL Tag Library >>
<< Messaging Tags | SQL Tag Library >>

Formatting Tags

The setBundle and bundle Tags
You can set the resource bundle at runtime with the JSTL fmt:setBundle and fmt:bundle tags.
fmt:setBundle
is used to set the localization context in a variable or configuration variable for a
specified scope. fmt:bundle is used to set the resource bundle for a given tag body.
The message Tag
The message tag is used to output localized strings. The following tag from
tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookcatalog.jsp is
used to output a string inviting customers to choose a book from the catalog.
<h3><fmt:message key=
"Choose"/></h3>
The param subtag provides a single argument (for parametric replacement) to the compound
message or pattern in its parent message tag. One param tag must be specified for each variable
in the compound message or pattern. Parametric replacement takes place in the order of the
param
tags.
Formatting Tags
JSTL provides a set of tags for parsing and formatting locale-sensitive numbers and dates.
The formatNumber tag is used to output localized numbers. The following tag from
tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookshowcart.jsp is
used to display a localized price for a book.
<fmt:formatNumber value=
"${book.price}" type="currency"/>
Note that because the price is maintained in the database in dollars, the localization is somewhat
simplistic, because the formatNumber tag is unaware of exchange rates. The tag formats
currencies but does not convert them.
Analogous tags for formatting dates (formatDate) and for parsing numbers and dates
(parseNumber, parseDate) are also available. The timeZone tag establishes the time zone
(specified with the value attribute) to be used by any nested formatDate tags.
In tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookreceipt.jsp, a
"pretend" ship date is created and then formatted with the formatDate tag:
<jsp:useBean id=
"now" class="java.util.Date" />
<jsp:setProperty name=
"now" property="time"
value=
"${now.time + 432000000}" />
<fmt:message key=
"ShipDate"/>
<fmt:formatDate value=
"${now}" type="date"
dateStyle=
"full"/>.
Internationalization Tag Library
Chapter 7 · JavaServer Pages Standard Tag Library
217