Java Reference
In-Depth Information
To define how the data is converted from the model view to the presentation view, the
Converter
implementation must implement the
getAsString(FacesContext,
UIComponent, Object)
method from the
Converter
interface. Here is an imple-
mentation of this method:
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value)
throws ConverterException {
String inputVal = null;
if ( value == null ) {
return null;
}
// value must be of a type that can be cast to a String.
try {
inputVal = (String)value;
} catch (ClassCastException ce) {
FacesMessage errMsg = new FacesMes-
sage(CONVERSION_ERROR_MESSAGE_ID);
FacesContext.getCurrentInstance().addMessage(null, errMsg);
throw new ConverterException(errMsg.getSummary());
}
// insert spaces after every four characters for better
// readability if they are not already present.
char[] input = inputVal.toCharArray();
StringBuilder builder = new StringBuilder(input.length + 3);
for ( int i = 0; i < input.length; ++i ) {
if ( (i % 4) == 0 && i != 0) {
if (input[i] != ' ' || input[i] != '-'){
builder.append(" ");
// if there are any "-"'s convert them to blanks.
} else if (input[i] == '-') {
builder.append(" ");
}
}
builder.append(input[i]);
}
String convertedValue = builder.toString();
return convertedValue;
}
During the Render Response phase, in which the components'
encode
methods are
called, the JavaServer Faces implementation calls the
getAsString
method in order