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.The standard
Converterimplementations, located in thejavax.faces.convertpackage, are as follows:Two of these standard converters (
DateTimeConverterandNumberConverter) have their own tags, which allow you to configure the format of the component data by configuring the tag attributes. Using DateTimeConverter discusses usingDateTimeConverter. Using NumberConverter discusses usingNumberConverter.You can use the other standard converters in one of three ways:
- You can make sure that the component that uses the converter has its value bound to a backing bean property of the same type as the converter.
- You can refer to the converter by class or by its ID using the component tag's
converterattribute. The ID is defined in the application configuration resource file (see Application Configuration Resource File).- You can refer to the converter by its ID using the
converterIdattribute of theconvertertag.The latter two will convert the component's local value. The first method will convert the model value of the component. For example, if you want a component's data to be converted to an
Integer, you can bind the component to a property similar to this:Integer age = 0; public Integer getAge(){ return age;} public void setAge(Integer age) {this.age = age;}Alternatively, if the component is not bound to a bean property, you can use the
converterattribute on the component tag:The data corresponding to this tag will be converted to a
java.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 refer to the converter's ID via theconvertertag'sconverterIdattribute. If the tag is referring to a custom converter, the value ofconverterIDmust match the ID in the application configuration resource file. Here is an example: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 18-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 18-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.