background image

Binding Component Values and Instances

<< Using the LongRangeValidator | Binding a Component Value to a Property >>
<< Using the LongRangeValidator | Binding a Component Value to a Property >>

Binding Component Values and Instances

Binding Component Values and Instances to External Data
Sources
As explained in
"Backing Beans" on page 309
, a component tag can wire its component's data to
a back-end data object by doing one of the following:
Binding its component's value to a bean property or other external data source
Binding its component's instance to a bean property
A component tag's value attribute uses a value expression to bind the component's value to an
external data source, such as a bean property. A component tag's binding attribute uses a value
expression to bind a component instance to a bean property.
When a component instance is bound to a backing bean property, the property holds the
component's local value. Conversely, when a component's value is bound to a backing bean
property, the property holds the value stored in the backing bean. This value is updated with the
local value during the update model values phase of the life cycle. There are advantages to both
of these techniques.
Binding a component instance to a bean property has these advantages:
The backing bean can programmatically modify component attributes.
The backing bean can instantiate components rather than let the page author do so.
Binding a component's value to a bean property has these advantages:
The page author has more control over the component attributes.
The backing bean has no dependencies on the JavaServer Faces API (such as the UI
component classes), allowing for greater separation of the presentation layer from the
model layer.
The JavaServer Faces implementation can perform conversions on the data based on the
type of the bean property without the developer needing to apply a converter.
In most situations, you will bind a component's value rather than its instance to a bean property.
You'll need to use a component binding only when you need to change one of the component's
attributes dynamically. For example, if an application renders a component only under certain
conditions, it can set the component's rendered property accordingly by accessing the property
to which the component is bound.
When referencing the property using the component tag's value attribute, you need to use the
proper syntax. For example, suppose a backing bean called MyBean has this int property:
int currentOption = null;
int getCurrentOption(){...}
void setCurrentOption(int option){...}
The value attribute that references this property must have this value-binding expression:
Binding Component Values and Instances to External Data Sources
The Java EE 5 Tutorial · September 2007
364