The standard converters supplied in the JavaServer Faces Reference Implementation provide the most common data type conversions. If you need to convert a component's data to a type other than a standard type, you can create your own custom converter.
It is beyond the scope of this topic to teach you how to write a custom
converter. Briefly, a custom converter must implement the
javax.faces.convert.Converter
interface. In the custom
implementation you must must define how component data is to be
converted between String and an object of the desired type, and override
the getAsObject
and getAsString
methods of the
Converter
interface. Use javax.faces.convert.ConverterException
to throw the appropriate exceptions and
javax.faces.application.FacesMessage
to construct the
corresponding error messages. For more information, see the Javadoc for
javax.faces.convert.Converter
, or go to the java.sun.com web page
http://java.sun.com/j2ee/javaserverfaces/
To use a custom converter in your application, you need to:
faces-config.xml
). You can register it under a
converter identifier (ID) or a fully qualified class name for a
specific data type.
converter
attribute on the component
or nest the f:converter
tag within the component.
Note: If a custom converter is registered in an
application under a class for a specific data type, whenever a
component's value references a value binding (see code snippet below)
that has the same type as the custom converter object, JSF will
automatically use the converter of that class to convert the data. In
that case you don't even have to use the converter
attribute or the f:converter
tag to register the custom
converter on a component.
<h:inputText value="#{myBean.myProperty}"/>
where myProperty
has the same type as the custom converter.
About the converter Attribute
Registering a Custom Converter or Validator in an Application
Registering a Custom Converter on a Component
Registering a Custom Converter Using a Value Binding Expression
Copyright © 1997, 2004, Oracle. All rights reserved.