Using the Standard Converters
The JavaServer Faces implementation provides a set of
Converterimplementations that you can use to convert component data. For more information on the conceptual details of the conversion model, see Conversion Model (page 289).The standard
Converterimplementations, located in thejavax.faces.convertpackage, are as follows:Each of these converters has a standard error message associated with them. If you have registered one of these converters onto a component on your page, and the converter is not able to convert the component's value, the converter's error message will display on the page. For example, the error message that displays if
BigIntegerConverterfails to convert a value is:In this case the
{0}substitution parameter will be replaced with the name of the input component on which the converter is registered. See section 2.4.5 of the JavaServer Faces specification, version 1.2, for a complete list of error messages.Two of the standard converters (
DateTimeConverterandNumberConverter) have their own tags, which allow you to configure the format of the component data using the tag attributes. Using DateTimeConverter discusses usingDateTimeConverter. Using NumberConverter discusses usingNumberConverter. The following section explains how to convert a component's value including how to register the other standard converters with a component.Converting a Component's Value
To use a particular converter to convert a component's value, you need to register the converter onto the component. You can register any of the standard converters on a component in one of four ways:
- Nest one of the standard converter tags inside the component's tag. These tags are
convertDateTimeandconvertNumberand are described in Using DateTimeConverter and Using NumberConverter, respectively.- Bind the value of the component to a backing bean property of the same type as the converter.
- Refer to the converter from the component tag's
converterattribute.- Nest a
convertertag inside of the component tag and use either theconvertertag'sconverterIdattribute or itsbindingattribute to refer to the converter.As an example of the second approach, if you want a component's data to be converted to an
Integer, you can simply bind the component's value to a property similar to this:Integer age = 0; public Integer getAge(){ return age;} public void setAge(Integer age) {this.age = age;}If the component is not bound to a bean property, you can employ the third technique by using the
converterattribute on the component tag:This example shows the
converterattribute referring to the fully-qualified class name of the converter. Theconverterattribute can also take the ID of the component. If the converter is a custom converter, the ID is defined in the application configuration resource file (see Application Configuration Resource File, page 450).The data corresponding to this example
inputTexttag will be converted to ajava.lang.Integer.Notice that theIntegertype is already a supported type of theNumberConverter. If you don't need to specify any formatting instructions using theconvertNumbertag attributes, and if one of the other converters will suffice, you can simply reference that converter using the component tag'sconverterattribute.Finally, you can nest a
convertertag within the component tag and use either the converter tag'sconverterIdattribute or itsbindingattribute to reference the converter.The
converterIdattribute must reference the converter's ID. Again, if the converter is a custom converter, the value ofconverterIDmust match the ID in the application configuration resource file; otherwise it must match the ID as defined in the converter class. Here is an example:Instead of using the
converterIdattribute, theconvertertag can use thebindingattribute. Thebindingattribute must resolve to a bean property that accepts and returns an appropriateConverterinstance. See Binding Converters, Listeners, and Validators to Backing Bean Properties for more information.Using DateTimeConverter
You can convert a component's data to a
java.util.Dateby nesting theconvertDateTimetag inside the component tag. TheconvertDateTimetag has several attributes that allow you to specify the format and type of the data. Table 10-5 lists the attributes.Here is a simple example of a
convertDateTimetag from thebookreceipt.jsppage:Here is an example of a date and time that this tag can display:
You can also display the same date and time using this tag:
<h:outputText value="#{cashier.shipDate}"> <f:convertDateTime pattern="EEEEEEEE, MMM dd, yyyy" /> </h:outputText>If you want to display the example date in Spanish, you can use the
localeattribute:<h:inputText value="#{cashier.shipDate}"> <f:convertDateTime dateStyle="full" locale="Locale.SPAIN" timeStyle="long" type="both" /> </h:inputText>This tag would display
Please refer to the Customizing Formats lesson of the Java Tutorial at
http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.htmlfor more information on how to format the output using thepatternattribute of theconvertDateTimetag.
Using NumberConverter
You can convert a component's data to a
java.lang.Numberby nesting theconvertNumbertag inside the component tag. TheconvertNumbertag has several attributes that allow you to specify the format and type of the data. Table 10-6 lists the attributes.The
bookcashier.jsppage of Duke's Bookstore uses aconvertNumbertag to display the total prices of the books in the shopping cart:Here is an example of a number this tag can display
This number can also be displayed using this tag:
<h:outputText id="cartTotal" value="#{cart.Total}" > <f:convertNumber pattern="$####"/> </h:outputText>Please refer to the Customizing Formats lesson of the Java Tutorial at
http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.htmlfor more information on how to format the output using the pattern attribute of theconvertNumbertag.