How to Add a Standard ADF Faces Converter

Validating and Converting Input 6-5

6.3.3 What Happens at Runtime

When the user submits the page containing converters, the ADF Faces validate method calls the converters getAsObject method to convert the String value to the required object type. When there is not an attached converter and if the component is bound to a bean property in the model, then ADF checks the models data type and attempts to find the appropriate converter. If conversion fails, the component’s value attribute is set to false and JSF adds an error message to a queue that is maintained by FacesContext. If conversion is successful and there are validators attached to the component, the converted value is passed to the validators. If no validators are attached to the component, the converted value is stored as a local value that is later used to update the model.

6.3.4 How to Add oracle.jbo.domain Converters

For oracle.jbo.domain datatypes that are not automatically converted, you will need to reference the oracle.jbo.domain converter in your component. These converters are automatically registered and do not need a tag. Table 6–2 lists the oracle.jbo.domain datatype converters. To add a oracle.jbo.domain converter, you can add the converter to the converter attribute as shown in Example 6–4 . Example 6–4 Adding genericDomain Converter using the converter attribute af:inputText ... converter=oracle.genericDomain Or you can add the f:converter tag and reference the converter using the converterId attribute as shown in Example 6–5 . Example 6–5 Adding genericDomain Converter using f:converter af:inputText ... f:converter converterId=oracle.genericDomain af:inputText

6.4 Creating Custom JSF Converters

You can create your own converters to meet your specific business needs. You can create custom JSF converters that run on the server-side using Java, and then also create a JavaScript version that can run on the client-side. However, unlike creating custom validators, you can create only converter classes. You cannot add a method to a backing bean to provide conversion.

6.4.1 How to Create a Custom JSF Converter

Creating a custom converter requires writing the business logic for the conversion by creating an implementation of the Converter interface that contains the getAsObject and getAsString methods, and then registering the custom Table 6–2 oracle.jbo.domain Datatype Converters oracle.jbo.domain Converter Description ordDomainConverter Handles oracle.jbo.domain.ord datatypes genericDomainConverter Handles generic oracle.jbo.domain datatypes 6-6 Web User Interface Developers Guide for Oracle Application Development Framework converter with the application. You then use the f:converter tag and set the custom converter as a property of that tag, or you can use the converter attribute on the input component to bind to that converter. You can also create a client-side version of the converter. ADF Faces client-side converters work in the same way standard JSF conversion works on the server, except that JavaScript is used on the client. JavaScript converter objects can throw ConverterException exceptions and they support the getAsObject and getAsString methods. To create a custom JSF converter: 1. Create a Java class that implements the javax.faces.converter.Converter interface. The implementation must contain a public no-args constructor, a set of accessor methods for any attributes, and getAsObject and getAsString methods to implement the Converter interface. The getAsObject method takes the FacesContext instance, the UI component, and the String value to be converted to a specified object, for example: public Object getAsObjectFacesContext context, UIComponent component, java.lang.String value{ .. } The getAsString method takes the FacesContext instance, the UI component, and the object to be converted to a String value. For example: public String getAsStringFacesContext context, UIComponent component, Object value{ .. } For more information about these classes, refer to the API documentation or visit http:download.oracle.comdocscdE17410_ 01javaeeindex.html . 2. Add the needed conversion logic. This logic should use javax.faces.convert.ConverterException to throw the appropriate exceptions and javax.faces.application.FacesMessage to generate the corresponding error messages. For more information about the Converter interface and the FacesMessage error handlers, see the API documentation for javax.faces.convert.ConverterException and javax.faces.application.FacesMessage, or visit http:download.oracle.comdocscdE17410_ 01javaeeindex.html . 3. If your application saves state on the client, your custom converter must implement the Serializable interface or the StateHolder interface, and the saveStateFacesContext and restoreStateFacesContext, Object methods of the StateHolder interface. For more information, see the Javadoc for the StateHolder interface of javax.faces.component package. 4. Register the converter in the faces-config.xml file. ■ Open the faces-config.xml file and select the Overview tab in the editor window. The faces-config.xml file is located in the View_ ProjectWEB-INF directory in the JDeveloper Application Navigator.