background image

Binding a Component Value to a Property

<< Binding Component Values and Instances | Binding a Component Value to an Implicit Object >>
<< Binding Component Values and Instances | Binding a Component Value to an Implicit Object >>

Binding a Component Value to a Property

#{MyBean.currentOption}
In addition to binding a component's value to a bean property, the value attribute can specify a
literal value or can map the component's data to any primitive (such as int), structure (such as
an array), or collection (such as a list), independent of a JavaBeans component.
Table 11­8
lists
some example value-binding expressions that you can use with the value attribute.
TABLE 11­8
Example Value-binding Expressions
Value
Expression
A Boolean
cart.numberOfItems > 0
A property initialized from a context init parameter
initParam.quantity
A bean property
CashierBean.name
Value in an array
books[3]
Value in a collection
books[
"fiction"]
Property of an object in an array of objects
books[3].price
The next two sections explain in more detail how to use the value attribute to bind a
component's value to a bean property or other external data sources and how to use the binding
attribute to bind a component instance to a bean property.
Binding a Component Value to a Property
To bind a component's value to a bean property, you specify the name of the bean and the
property using the value attribute. As explained in
"Backing Beans" on page 309
, the value
expression of the component tag's value attribute must match the corresponding managed
bean declaration in the application configuration resource file.
This means that the name of the bean in the value expression must match the
managed-bean-name
element of the managed bean declaration up to the first period (.) in the
expression. Similarly, the part of the value expression after the period must match the name
specified in the corresponding property-name element in the application configuration
resource file.
For example, consider this managed bean configuration, which configures the ImageArea bean
corresponding to the North America part of the image map on the chooselocale.jsp page of
the Duke's Bookstore application:
<managed-bean>
<managed-bean-name> NA </managed-bean-name>
<managed-bean-class> model.ImageArea </managed-bean-class>
Binding Component Values and Instances to External Data Sources
Chapter 11 · Using JavaServer Faces Technology in JSP Pages
365