HTML and CSS Reference
In-Depth Information
As shown in the
Converter
interface, it contains two APIs:
The first API is
getAsObject
, which performs the String-to-Object conversion; this API can be called in the
Process Validations phase (or the Apply Request Values phase). It has three parameters:
1.
context
, which represents the JSF
FacesContext
instance of this request.
2.
component
, which represents the component whose value will be converted.
This component instance can be used in order to retrieve the component attributes
if the converter needs to use them.
3.
value
, which represents the
String
value to be converted to
Object
.
The second API is
getAsString
, which performs the Object-to-String conversion; this API is called in the Render
Response phase. It takes three parameters:
1.
context
, which represents the JSF
FacesContext
instance of this request.
2.
component
, which represents the component whose value will be converted.
This component instance can be used in order to retrieve the component attributes
if the converter needs to use them.
3.
value
, which represents the
Object
value to be converted to
String
.
Every component can have one or more converter(s). If conversion could not be performed due to an error,
the converter must throw
ConverterException
; in this case, the component that owns the converter will be marked
as invalid, and
ConverterException
message will be received and added to
FacesContext
messages in order to be
displayed by the
<h:message>
and
<h:messages>
components. Listing 3-2 shows an example illustrating how JSF
conversion works for multiple
ValueHolder
components with attached converters.
Listing 3-2.
Conversion Example for Multiple ValueHolder Components
<h:form>
<h1>Test form</h1>
<h:outputText value="Enter First Number: "/>
<h:inputText id="firstNumber"
value="#{testBean.firstNumber}">
<f:convertNumber/>
</h:inputText>
<h:message for="firstNumber"/>
<br/>
<h:outputText value="Enter Second Number: "/>
<h:inputText id="secondNumber"
value="#{testBean.secondNumber}">
<f:convertNumber/>
</h:inputText>
<h:message for="secondNumber"/>
<br/>
<h:commandButton value="submit"/>
</h:form>
In this example, we have a form that contains two
inputText
components. Every
inputText
component has an
attached
<f:convertNumber>
converter.
<f:convertNumber/>
converter converts the user's entered value in the input
text to a Java
Number
object, which is then binded with the attribute (
firstNumber
and
secondNumber)
of
TestBean
managed bean. If the user enters non-numeric values in both
inputText
components and then clicks the
"submit"
button, the user will see two conversion error messages in both the
<h:message/>
components which are attached to