Download
FAQ History |
API
Search Feedback |
Using the Standard Converters
The JavaServer Faces implementation provides a set of
Converter
implementations that you can use to convert component data. For more information on the conceptual details of the conversion model, see Conversion Model.The standard
Converter
implementations, located in thejavax.faces.convert
package, are as follows:Two of these standard converters (
DateTimeConverter
andNumberConverter
) 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
converter
attribute. 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
converterId
attribute of theconverter
tag.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
converter
attribute on the component tag:The data corresponding to this tag will be converted to a
java.lang.Integer.
Notice that theInteger
type is already a supported type of theNumberConverter
. If you don't need to specify any formatting instructions using theconvertNumber
tag attributes, and if one of the other converters will suffice, you can simply reference that converter using the component tag'sconverter
attribute.Finally, you can nest a
converter
tag within the component tag and refer to the converter's ID via theconverter
tag'sconverterId
attribute. If the tag is referring to a custom converter, the value ofconverterID
must 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.Date
by nesting theconvertDateTime
tag inside the component tag. TheconvertDateTime
tag 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
convertDateTime
tag from thebookreceipt.jsp
page: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 d, yyyy" </h:outputText>
If you want to display the example date in Spanish, you can use the
parseLocale
attribute:<h:inputText value="#{cashier.shipDate}"> <f:convertDateTime dateStyle="full" parseLocale="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.html
for more information on how to format the output using thepattern
attribute of theconvertDateTime
tag.
Using NumberConverter
You can convert a component's data to a
java.lang.Number
by nesting theconvertNumber
tag inside the component tag. TheconvertNumber
tag has several attributes that allow you to specify the format and type of the data. Table 18-6 lists the attributes.The
bookcashier.jsp
page of Duke's Bookstore uses aconvertNumber
tag 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="netIncome" 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.html
for more information on how to format the output using thepattern
attribute of theconvertNumber
tag.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.