background image

Using NumberConverter

<< Using DateTimeConverter | convertNumber Attributes >>
<< Using DateTimeConverter | convertNumber Attributes >>

Using NumberConverter

Please refer to the Customizing Formats lesson of the Java Tutorial at
http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html
for
more information on how to format the output using the pattern attribute of the
convertDateTime
tag.
TABLE 11­5
convertDateTime
Tag Attributes
Attribute
Type
Description
binding
DateTimeConverter
Used to bind a converter to a backing bean property
dateStyle
String
Defines the format, as specified by java.text.DateFormat, of a date or
the date part of a date string. Applied only if type is date (or both) and
pattern
is not defined. Valid values: default, short, medium, long, and
full
. If no value is specified, default is used.
locale
String
or Locale
Locale
whose predefined styles for dates and times are used during
formatting or parsing. If not specified, the Locale returned by
FacesContext.getLocale
will be used.
pattern
String
Custom formatting pattern that determines how the date/time string
should be formatted and parsed. If this attribute is specified, dateStyle,
timeStyle
, and type attributes are ignored.
timeStyle
String
Defines the format, as specified by java.text.DateFormat, of a time or
the time part of a date string. Applied only if type is time and pattern is
not defined. Valid values: default, short, medium, long, and full. If no
value is specified, default is used.
timeZone
String
or TimeZone
Time zone in which to interpret any time information in the date string.
type
String
Specifies whether the string value will contain a date, a time, or both.
Valid values are date, time, or both. If no value is specified, date is used.
Using NumberConverter
You can convert a component's data to a java.lang.Number by nesting the convertNumber tag
inside the component tag. The convertNumber tag has several attributes that allow you to
specify the format and type of the data.
Table 11­6
lists the attributes.
The bookcashier.jsp page of Duke's Bookstore uses a convertNumber tag to display the total
prices of the books in the shopping cart:
<h:outputText value=
"#{cart.total}" >
<f:convertNumber type=
"currency"/>
</h:outputText>
When binding the Number converter to a component, ensure that the backing bean property to
which the component is bound is of primitive type or has a type of java.lang.Number. In the
case of the preceding example, cart.total is of type java.lang.Number.
Using the Standard Converters
Chapter 11 · Using JavaServer Faces Technology in JSP Pages
357