Download
FAQ History |
API
Search Feedback |
Binding Component Values and Instances to External Data Sources
As explained in Backing Bean Management, a component tag can wire its component's data to a back-end data object by doing one of the following:
A component tag's
value
attribute uses a value-binding expression to bind a component's value to an external data source, such as a bean property. A component tag'sbinding
attribute uses a value-binding expression to bind a component instance to a bean property.When referencing the property using the component tag's
value
attribute, you need to use the proper syntax. For example, suppose a backing bean calledMyBean
has thisString
property:int currentOption = null;
int getCurrentOption(){...}
void setCurrentOption(int option){...}The value attribute that references this property must have this value-binding expression:
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 asint
), structure (such as an array), or collection (such as a list), independent of a JavaBeans component. Table 18-8 lists some example value-binding expressions that you can use with thevalue
attribute.
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 thebinding
attribute to bind a component instance to a bean propertyBinding 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 Bean Management, the value-binding expression of the component tag'svalue
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-binding expression must match the
managed-bean-name
element of the managed bean declaration up to the first . in the expression. Similarly, the part of the value-binding expression after the . must match the name specified in the correspondingproperty-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 thechooselocale.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> <managed-bean-scope> application </managed-bean-scope> <managed-property> <property-name>shape</property-name> <value>poly</value> </managed-property> <managed-property> <property-name>alt</property-name> <value>NAmerica</value> </managed-property> ... </managed-bean>This example configures a bean called
NA
, which has several properties, one of which is calledshape
.Although the
area
tags on thechooselocale.jsp
page do not bind to anImageArea
property (they bind to the bean itself), to do this, you refer to the property using a value-binding expression from thevalue
attribute of the component's tag:Much of the time you will not include definitions for a managed bean's properties when configuring it. You need to define a property and its value only when you want the property to be initialized with a value when the bean is initialized.
If a component tag's
value
attribute must refer to a property that is not initialized in themanaged-bean
configuration, the part of the value-binding expression after the . must match the property name as it is defined in the backing bean.See Application Configuration Resource File for information on how to configure beans in the application configuration resource file.
Writing Component Properties explains in more detail how to write the backing bean properties for each of the component types.
Binding a Component Value to an Implicit Object
One external data source that a
value
attribute can refer to is an implicit object.The
bookreceipt.jsp
page of the Duke's Bookstore application includes a reference to an implicit object from a parameter substitution tag:<h:outputFormat title="thanks" value="#{bundle.ThankYou}"> <f:param value="#{sessionScope.name}"/> </h:outputFormat>This tag gets the name of the customer from the session scope and inserts it into the parameterized message at the key
ThankYou
from the resource bundle. For example, if the name of the customer is Gwen Canigetit, this tag will render:The
name
tag on thebookcashier.jsp
page has theNameChanged
listener implementation registered on it. This listener saves the customer's name in the session scope when thebookcashier.jsp
page is submitted. See Implementing Value-Change Listeners for more information on how this listener works. See Registering a ValueChangeListener on a Component to learn how the listener is registered on the tag.Retrieving values from other implicit objects is done in a similar way to the example shown in this section. Table 18-9 lists the implicit objects that a value attribute can refer to. All of the implicit objects except for the scope objects are read-only and therefore should not be used as a value for a
UIInput
component.
Binding a Component Instance to a Bean Property
A component instance can be bound to a bean property using a value-binding expression that contains the
binding
attribute of the component's tag. You usually bind a component instance rather than its value to a bean property if the bean must dynamically change the component's attributes.Here are two tags from the
bookcashier.jsp
page that bind components to bean properties:<h:selectBooleanCheckbox id="fanClub" rendered="false" binding="#{cashier.specialOffer}" /> <h:outputLabel for="fanClubLabel" rendered="false" binding="#{cashier.specialOfferText}" > <h:outputText id="fanClubLabel" value="#{bundle.DukeFanClub}" /> </h:outputLabel>The
selectBooleanCheckbox
tag renders a checkbox and binds thefanClub
UISelectBoolean
component to thespecialOffer
property ofCashierBean
. TheoutputLabel
tag binds the component representing the checkbox's label to thespecialOfferText
property ofCashierBean
. If the application's locale is English, theoutputLabel
tag renders:
I'd like to join the Duke Fan Club, free with my purchase of over $100
The
rendered
attributes of both tags are set tofalse
, which prevents the checkbox and its label from being rendered. If the customer orders more than $100 (or 100 euros) worth of books and clicks theSubmit
button, thesubmit
method ofCashierBean
sets both components'rendered
properties totrue
, causing the checkbox and its label to be rendered.These tags use component bindings rather than value bindings because the backing bean must dynamically set the values of the components'
rendered
properties.If the tags were to use value bindings instead of component bindings, the backing bean would not have direct access to the components, and would therefore require additional code to access the components from the
FacesContext
to change the components'rendered
properties.Writing Properties Bound to Component Instances explains how to write the bean properties bound to the example components and also discusses how the
submit
method sets therendered
properties of the components.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.