Sunday, January 29, 2012

Bean Properties


As explained in Backing Beans, a backing bean property can be bound to one of the following items:
  • A component value
  • A component instance
  • A Converter implementation
  • A Listener implementation
  • A Validator implementation
These properties follow JavaBeans component conventions.
The UI component's tag binds the component's value to a property using its value attribute and binds the component's instance to a property using its binding attribute, as explained in Binding Component Values and Instances to External Data Sources. Likewise, all the converter, listener, and validator tags use their binding attributes to bind their associated implementations to backing bean properties, as explained in Binding Converters, Listeners, and Validators to Backing Bean Properties.
To bind a component's value to a backing bean property, the type of the property must match the type of the component's value to which it is bound. For example, if a backing bean property is bound to a UISelectBoolean component's value, the property should accept and return a boolean value or a Boolean wrapper Object instance.
To bind a component instance, the property must match the component type. For example, if a backing bean property is bound to a UISelectBoolean instance, the property should accept and return UISelectBoolean.
Similarly, in order to bind a converter, listener, or validator implementation to a property, the property must accept and return the same type of converter, listener, or validator object. For example, if you are using the convertDateTime tag to bind a DateTime converter to a property, that property must accept and return a DateTime instance.
The rest of this section explains how to write properties that can be bound to component values, to component instances for the component objects described in Adding UI Components to a Page Using the HTML Component Tags, and to converter, listener, and validator implementations.
Acceptable Types of Component Values 
Component
Acceptable Types of Component Values
UIInput, UIOutput, UISelectItem, UISelectOne
Any of the basic primitive and numeric types or any Java programming language object type for which an appropriate Converter implementation is available.
UIData
array of beans, List of beans, single bean, java.sql.ResultSet, javax.servlet.jsp.jstl.sql.Result, javax.sql.RowSet.
UISelectBoolean
boolean or Boolean.
UISelectItems
java.lang.String, Collection, Array, Map.
UISelectMany
array or List. Elements of the array or List can be any of the standard types

UIInput and UIOutput Properties

The following tag binds the name component to the name property of CashierBean.
<h:inputText id="name" size="50"
  value="#{cashier.name}"
  required="true"> 
  <f:valueChangeListener 
    type="com.sun.bookstore6.listeners.NameChanged" /> 
</h:inputText> 
Here is the bean property bound to the name component:
protected String name = null; 
public void setName(String name) {
  this.name = name;
}
public String getName() {
  return this.name;
}  
As Using the Standard Converters describes, to convert the value of a UIInput or UIOutput component, you can either apply a converter or create the bean property bound to the component with the desired type. Here is the example tag explained in Using DateTimeConverter that displays the date books will be shipped:
<h:outputText value="#{cashier.shipDate}">
  <f:convertDateTime dateStyle="full" />
</h:outputText> 
The application developer must ensure that the property bound to the component represented by this tag has a type of java.util.Date. Here is the shipDate property in CashierBean:
protected Date shipDate;
public Date getShipDate() {
  return this.shipDate;
}
public void setShipDate(Date shipDate) {
  this.shipDate = shipDate;
} 

No comments:

Post a Comment