How to Set Attributes on a Standard ADF Faces Converter

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. Validating and Converting Input 6-7 ■ In the window, select Converters and click New. Click Help or press F1 for additional help in registering the converter. To create a client-side version of the converter: ■ Write a JavaScript version of the converter, passing relevant information to a constructor. Example 6–6 shows the code to implement the interface org.apache.myfaces.trinidad.convert.ClientConverter, which has two methods. The first method is getClientScript, which returns an implementation of the JavaScript Converter object. The second method is getClientConversion, which returns a JavaScript constructor that is used to instantiate an instance of the converter. Example 6–6 Interface Converter function TrConverter { } Convert the specified model object value, into a String for display param value Model object value to be converted param label label to identify the editableValueHolder to the user return the value as a string or undefined in case of no converter mechanism is available see TrNumberConverter. TrConverter.prototype.getAsString = functionvalue, label{} Convert the specified string value into a model data object which can be passed to validators param value String value to be converted param label label to identify the editableValueHolder to the user return the converted value or undefined in case of no converter mechanism is available see TrNumberConverter. TrConverter.prototype.getAsObject = functionvalue, label{} The TrConverter interface can throw a TrConverterException exception, which should contain a TrFacesMessage error message. Example 6–7 shows the signature for TrFacesMessage and Example 6–8 shows the signature for TrFacesException. Example 6–7 TrFacesMessage Signature Message similar to javax.faces.application.FacesMessage param summary - Localized summary message text param detail - Localized detail message text param severity - An optional severity for this message. Use constants SEVERITY_INFO, SEVERITY_WARN, SEVERITY_ERROR, and SEVERITY_FATAL from the FacesMessage class. Default is SEVERITY_INFO function TrFacesMessage summary, detail, severity