JSF Interview Questions
- Which type of converters can we use in our application?
A JSF application can use two types of converters :
- JSF standard Converters
JSF supplies built-in converters known as standard converters. All standard converters implements javax.faces.convert.Converter interface. These converter classes are listed below :
- BigDecimalConverter
- BigIntegerConverter
- BooleanConverter
- ByteConverter
- CharacterConverter
- DateTimeConverter
- DoubleConverter
- FloatConverter
- IntegerConverter
- LongConverter
- NumberConverter
- ShortConverter
- Custom Converter
Custom data converter is useful in in converting field data into an application-specific value object. For example,
- String to User object.
- String to Product object etc.
- JSF standard Converters
- Which standard converters have their own tags?
DateTimeConverter and NumberConverter are two standard converters that have their own tags convertDateTime and convertNumber respectively that help configuring the format of the component data by configuring tag attributes.
DateTimeConverter : A component data can be converted to a Date object using convertDateTime tag within the component tag like,
<h:inputText value="#{Bean.RegisterDate}">
<f:convertDateTime pattern="MMM,dd,YYYY" />
</h:inputText>NumberConverter : Converter for dealing with numbers such as type of number, maximum integer digits, currency symbol etc.
<h:inputText id="it" value="#{Bean.value}">
<f:convertNumber maxFractionDigits="3"
type="currency"
currencySymbol="$"
maxIntegerDigits="4" />
</h:inputText> - What are the ways of using standard converters of JSF?
Standard converters other than the two which have their own tags (DateTimeConverter and NumberConverter) can be used in one of the following three ways. The first one converts the model value of the component and the other two ways convert the component's local value.
-
Bind UI Component to Backing Bean Property :
Make sure that the component has its value bound to a backing bean property of the same type as the converter. For example, converting component data to a float number requires binding the component to the property like :
Float value = 0.0;
public Float getValue(){ return value;}
public void setValue(Float value) {this.value = value;} - Use ?converter? attribute on the UI Component :
Using the converter attribute of the component tag. Just specify the fully qualified class name or ID of the converter in the converter attribute. If the component is not bound to a bean property then this option can be used. For example,
<h:inputText converter="javax.faces.Integer" />
- Use <f:converter> Tag with ConverterId Attribute :
Using converter tag within the component tag and refer the converter by specifying the ID of the converter in the convertId attribute. For example:
<h:inputText value="#{Bean.Value}" />
<f:converter converterId="Float" />
</h:inputText>
-
Bind UI Component to Backing Bean Property :