Java Reference
In-Depth Information
To define how the data is converted from the presentation view to the model view, the
Converter
implementation must implement the
getAsObject(FacesContext,
UIComponent, String)
method from the
Converter
interface. Here is the imple-
mentation of this method from
CreditCardConverter
:
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String newValue)
throws ConverterException {
String convertedValue = null;
if ( newValue == null ) {
return newValue;
}
// Since this is only a String to String conversion,
// this conversion does not throw ConverterException.
convertedValue = newValue.trim();
if ( (convertedValue.contains("-")) ||
(convertedValue.contains(" "))) {
char[] input = convertedValue.toCharArray();
StringBuilder builder = new StringBuilder(input.length);
for ( int i = 0; i < input.length; ++i ) {
if ( input[i] == '-' || input[i] == ' ' ) {
continue;
} else {
builder.append(input[i]);
}
}
convertedValue = builder.toString();
}
return convertedValue;
}
During the Apply Request Values phase, when the components'
decode
methods are
processed, the JavaServer Faces implementation looks up the component's local value
in the request and calls the
getAsObject
method. When calling this method, the
JavaServer Faces implementation passes in the current
FacesContext
instance, the
component whose data needs conversion, and the local value as a
String
. The method
then writes the local value to a character array, trims the hyphens and blanks, adds the rest
of the characters to a
String
, and returns the
String
.