Conversion is the process whereby component data is transformed from strings to Java objects and vice versa, depending on whether the data is being sent from the client browser to the application server, or from the server to the browser.
For example a date field in a web form might represent a
java.util.Date
object as a text string in the format pattern
yyyy/mm/dd
. When a user edits a date field and submits the form, the
string must be converted back to the type that is required by the
application.
The JavaServer Faces (JSF) implementation provides standard converters
that handle conversion between strings and simple data types (e.g.,
numbers, Boolean). Implementing the javax.faces.convert.Converter
interface, the supplied JSF converter classes are:
Note: JSF automatically converts component data when the component value binding type is a primitive type, BigDecimal, or BigInteger. For Date values, you need to add an explicit converter because you can specify the formatting style to convert to for date and time portions.
Two of the standard converter classes have their own tags, which let you specify the format and type of the component data by configuring the tag attributes. The standard converter classes that have their own tags are registered in JSF under a symbolic name:
Tag | Class | Name | For conversion between... |
---|---|---|---|
f:convertDateTime |
DateTimeConverter
|
javax.faces.DateTime |
String and java.util.Date values
|
f:convertNumber |
NumberConverter
|
javax.faces.Number |
String and java.lang.Number values
|
To use a standard tag, nest the supplied tag within a component. Use the tag's attributes to configure the format and type of component data.
For information about using standard tags, see Registering a Standard Converter on a Component Using a Standard Tag. For a list of the supported tag attributes, see Reference: Standard JSF Converter and Validator Tags.
The rest of the built-in converters do not have their own tags. These standard converters are registered in JSF for a specific data type:
Type | Class | Name | For conversion between... |
---|---|---|---|
BigDecimal |
BigDecimalConverter
|
javax.faces.BigDecimal |
String and java.math.BigDecimal values
|
BigInteger |
BigIntegerConverter
|
javax.faces.BigInteger |
String and java.math.BigInteger values
|
Boolean and boolean |
BooleanConverter
|
javax.faces.Boolean |
String and java.lang.Boolean (and boolean primitive)
values
|
Byte and byte |
ByteConverter
|
javax.faces.Byte |
String and java.lang.Number values
|
Character and char |
CharacterConverter
|
javax.faces.Character |
String and java.lang.Character (and char primitive)
values
|
Double and double |
DoubleConverter
|
javax.faces.Double |
String and java.lang.Double (and double primitive)
values
|
Float and float |
FloatConverter
|
javax.faces.Float |
String and java.lang.Float (and float primitive) values
|
Integer and int |
IntegerConverter
|
javax.faces.Integer |
String and java.lang.Integer (and int primitive) values
|
Long and long |
LongConverter
|
javax.faces.Long |
String and java.lang.Long (and long primitive) values
|
Short and short |
ShortConverter
|
javax.faces.Short |
String and java.lang.Short (and short primitive) values
|
You can use standard converters that do not have supplied tags in one of three ways:
converter
attribute to a component using the
fully qualified class name or name of the converter as the value.
f:converter
tag within a component. Use this
tag's converterID
attribute to reference the converter's
name.
For information about using standard converters without standard tags, see Registering a Standard Converter That Does Not Have A Supplied Tag.
If you need to convert a component's data to a type other than a standard type supported in JSF, or when you want to convert the format of the data, you can create your own custom converter. See Working with Custom JSF Converters and Validators for information about creating and registering custom converters for your application.
About the Conversion and Validation
Process in JSF
About Conversion and
Validation Errors
About the
converter Attribute
Working
with Standard JSF Converters and Validators
Copyright © 1997, 2004, Oracle. All rights reserved.