Binding Converters, Listeners, and Validators to Backing Bean Properties


As described previously in this chapter, a page author can bind converter, listener, and validator implementations to backing bean properties using the binding attributes of the tags used to register the implementations on components.

This technique has similar advantages to binding component instances to backing bean properties, as described in Binding Component Values and Instances to External Data Sources (page 371). In particular, binding a converter, listener, or validator implementation to a backing bean property yields the following benefits:

  • The backing bean can instantiate the implementation instead of allowing the page author to do so.

  • The backing bean can programmatically modify the attributes of the implementation. In the case of a custom implementation, the only other way to modify the attributes outside of the implementation class would be to create a custom tag for it and require the page author to set the attribute values from the page.

Whether you are binding a converter, listener, or validator to a backing bean property, the process is the same for any of the implementations:

  • Nest the converter, listener, or validator tag within an appropriate component tag.

  • Make sure that the backing bean has a property that accepts and returns the converter, listener, or validator implementation class that you want to bind to the property.

  • Reference the backing bean property using a value expression from the binding attribute of the converter, listener, or validator tag.

For example, say that you want to bind the standard DateTime converter to a backing bean property because the application developer wants the backing bean to set the formatting pattern of the user's input rather than let the page author do it. First, the page author registers the converter onto the component by nesting the convertDateTime tag within the component tag. Then, the page author references the property with the binding attribute of the convertDateTime tag:

   <h:inputText value="#{LoginBean.birthDate}">      <f:convertDateTime binding="#{LoginBean.convertDate}" />    </h:inputText>


The convertDate property would look something like this:

   private DateTimeConverter convertDate;    public DateTimeConverter getConvertDate() {      ...      return convertDate;    {    public void setConvertDate(DateTimeConverter convertDate) {      convertDate.setPattern("EEEEEEEE, MMM dd, yyyy");      this.convertDate = convertDate;    }


See Writing Properties Bound to Converters, Listeners, or Validators (page 401) for more information on writing backing bean properties for converter, listener, and validator implementations.



The JavaT EE 5 Tutorial
The JavaT EE 5 Tutorial
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 309

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net