Binding converters to backing bean properties

JSF standard converter tags allow binding attributes (this is also true for listener and validator tags). This means that developers can bind converter implementations to backing bean properties. The main advantages of using the binding facility are:

  • The developer can allow the backing bean to instantiate the implementation
  • The backing bean can programmatically access the implementation's attributes

Getting ready

We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

To successfully accomplish a binding task, you can follow the three simple steps listed next (these steps are true for converter, listener, and validator tags):

  1. Naest the converter (listener, validator) tag in the component tag.
  2. Put in the backing bean a property that takes and returns the converter (listener, validator) implementation class.
  3. Reference the backing bean property using a value expression from the binding attribute of the converter (listener, validator) tag.

For example, let's bind the standard convertNumber converter to a backing bean property. The idea is to let the backing bean set the formatting pattern of the user's input. First, you have to register the converter onto the component by nesting the convertNumber tag within the component tag. Then, you have to reference the property with the binding attribute of the convertNumber tag, as shown next:

<h:form id="numberFormID">
<h:inputText id="numberID" value="#{numbersBean.numbery}">
<f:convertNumber binding="#{numbersBean.number}" />
</h:inputText>
<h:message showSummary="true" showDetail="false" for="numberID"
style="color: red; text-decoration:overline"/>
<br />
<h:commandButton value="Submit"
action="selected?faces-redirect=true"/>
</h:form>

The number property would be similar to the following code:

package numbers;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.convert.NumberConverter;
@ManagedBean
@SessionScoped
public class NumbersBean {
private NumberConverter number;
private float numbery;
public float getNumbery(){
return this.numbery;
}
public void setNumbery(float numbery){
this.numbery=numbery;
}
public NumberConverter getNumber(){
return this.number;
}
public void setNumber(NumberConverter number){
number.setType("currency");
number.setCurrencySymbol("$");
this.number=number;
}
}

How it works...

In our example, the backing bean sets the formatting pattern within the convertNumber tag, which means that the user's input will be constrained to this pattern. This time the numbers are formatted as currencies, without using specific attributes in the convertNumber tag. Instead of this we use the binding attribute to reference the number property, which is a NumberConverter instance, offering us access to this class's methods.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and is named: Bind_converters_to_backing_bean_properties.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset