HTML and CSS Reference
In-Depth Information
FacesMessage message = new FacesMessage("Invalid Location format (address, city, country).",
"Use the following format {address, city, country)}.");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ConverterException(message);
}
String address = locationParts[0];
String city = locationParts[1];
String country = locationParts[2];
Location location = new Location(address, city, country);
return location;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Location location = (Location) value;
return location.getAddress() + ", " +
location.getCity() + ", " +
location.getCountry();
}
}
In the
getAsObject()
method which is called in the
Process Validations phase
(or in
Apply Request Values phase
),
the conversion from the input
String
is converted to the
Location
object whose class is mentioned in Listing 3-6, and
as shown in the bolded lines, if the input String does not meet the location format specification, a
ConverterException
with a faces error message is thrown. The location format is specified to be on the following form:
Address, City, Country
In
getAsString()
method which is called in the
Render Response phase
, the conversion from the
Location
object to the output rendering String is performed. It is important to notice the
@FacesConverter
annotation, which
is used for registering the converter in the JSF application. The
@FacesConverter
annotation has two main attributes:
value()
attribute, which is taken to be the converter ID, and the
forClass()
attribute, which is taken to be the
converter for class. For this example, we used only the
value()
attribute and declared our converter ID to be
"com.jsfprohtml5.LocationConverter"
.
Instead of using
@FacesConverter
annotation, you can declare the converter in the JSF
faces-config.xml
file
as follows:
<faces-config ...>
<converter>
<converter-id>com.jsfprohtml5.LocationConverter</converter-id>
<converter-class>com.jsfprohtml5.example.converters.LocationConverter</converter-class>
</converter>
</faces-config>