HTML and CSS Reference
In-Depth Information
Using the
type="percent"
attribute of the
<f:convertNumber>
, you can format the number as a percentage.
<h:outputText value="#{testBean.someNumber}">
<f:convertNumber type="percent"/>
</h:outputText>
If the
#{testBean.someNumber}
is evaluated to
0.3
for example, it will be displayed as
30%
.
■
notice that you can override the different conversion messages by using the
converterMessage
attribute of
EditableValueHolder
s. For example: <h:inputtext id=“somenumber” value=“#{bean.somenumber}”
converterMessage=“not a number!!!”/>
Note
Will show a conversion error message “not a number” if the number conversion fails.
Building Custom JSF Converter
In addition to all of the mentioned implicit and explicit converters provided by the JSF framework, JSF allows developers
to create their own custom converters. Let's see an example to illustrate this idea. Assume we need to convert the
user input
String
to a
Location
object. In order to achieve this requirement, we need to develop a custom converter
(
LocationConverter
for example) which converts the input
String
to a
Location
object and then converts the
Location
object to a friendly String that can be displayed to the end user in the
Render Response phase
. Listing 3-6 shows the
Location
class that we need the user input to be converted to.
Listing 3-6.
Location Class
package com.jsfprohtml5.example.model;
public class Location {
private String address;
private String city;
private String country;
public Location() {
}
public Location(String address, String city, String country) {
this.address = address;
this.city = city;
this.country = country;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
