|
Download
FAQ History |
|
API
Search Feedback |
Binding a Component to a Data Source
The
UIInputandUIOutputcomponents (and all components that extend these components) support storing a local value and referring to a value in another location with the optionalvalueRefattribute, which has replaced themodelReferenceattribute of previous releases. Like themodelReferenceattribute, thevalueRefattribute is used to bind a component's data to data stored in another location.Also like the
modelReferenceattribute, thevalueRefattribute can be used to bind a component's data to a JavaBeans component or one of its properties. What's different about thevalueRefattribute is that it also allows you to 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.In addition to the
valueRefattribute, this release also introduces theactionRefattribute, which binds anActionto a component. As explained in section Navigating Between Pages, anActionperforms some logic and returns an outcome, which tells the navigation model what page to access next.This section explains how the binding of a component to data works, and how to use
valueRefto bind a component to a bean property and primitive, and how to combine the component data with anAction.How Binding a Component to Data Works
Many of the standard components support storing local data, which is represented by the component's
valueproperty. They also support referencing data stored elsewhere, represented by the component'svalueRefproperty.Here is an example of using a
valueproperty to set an integer value:Here is an example of using a
valueRefproperty to refer to the bean property that stores the same integer:During the Apply Request Values phase of the standard request processing lifecycle, the component's local data is updated with the values from the current request. During this phase and the Process Validations phase, local values from the current request are checked against the converters and validators registered on the components
During the Update Model Values phase, the JavaServer Faces implementation copies the component's local data to the model data if the component has a valueRef property that points to a model object property.
During the Render Response phase, model data referred to by the component's
valueRefproperty is accessed and rendered to the page.The
valueRefproperty uses an expression language syntax to reference the data bound to a component. Table 21-6 shows a few examples of validvalueRefexpressions.
The new
ValueBindingAPI evaluates thevalueRefexpression that refers to a model object, a model object property, or other primitive or data structure.A
ValueBindinguses aVariableResolverto retrieve a value. TheVariableResolversearches the scopes and implicit objects to retrieve the value. Implicit objects map parameters to values. For example, the integer literal, quantity, from Table 21-6 is initialized as a property that is initialized from a context init parameter. The implicit objects that aVariableResolversearches are listed in Table 21-7.
A
VariableResolveralso creates and stores objects in scope. The defaultVariableResolverresolves standard implicit variables and is the Managed Bean Facility, discussed in section Creating Model Objects. The Managed Bean Facility is configured with the application configuration resource file.It's also possible to create a custom
VariableResolver. There are many situations in which you would want to create aVariableResolver. One situation is if you don't want the web application to search a particular scope, or you want it to search only some of the scopes for performance purposes.Binding a Component to a Bean Property
To bind a component to a bean or its property, you must first specify the name of the bean or property as the value of the
valueRefattribute. You configured this bean in the application configuration file, as explained in section Creating Model Objects. If you are binding the component to a bean or its property, the component tag'svalueRefexpression must match the correspondingmessage-bean-nameelement up to the first "." in the expression. Likewise, the part of thevalueRefexpression after the "." must match the name specified in the correspondingproperty-nameelement in the application configuration file. For example, consider this bean configuration:<managed-bean> <managed-bean-name>CarBean</managed-bean-name> <managed-property> <property-name>carName</property-name> <value>Jalopy</value> </managed-property> ... </managed-bean>This example configures a bean called
CarBean, which has a property calledcarNameof type String. If there is already a matching instance of this bean in the specified scope, the JavaServer Faces implementation does not create it.To bind a component to this bean property, you refer to the property using a reference expression from the
valueRefattribute of the component's tag:See section Creating Model Objects for information on how to configure beans in the application configuration file.
Writing Model Object Properties explains in more detail how to write the model object properties for each of the component types.
Binding a Component to an Initial Default
As explained in How Binding a Component to Data Works, the
valueRefproperty can refer to a value mapped in an implicit object.Suppose that you have a set of pages that all display a version number in a
UIOutputcomponent. You can save this number in an implicit object. This way, all of the pages can reference it, rather than each page including it. To saveversionNoas an initial default value in the contextinitParamimplicit object set thecontext-paramelement in yourweb.xmlfile:To access the version number at the time the page is rendered, refer to the parameter from the
versioncomponent tag'svalueRefattribute:Storing values to and retrieving values from other implicit objects are done in a similar way.
Combining Component Data and Action Objects
An
Actionis an object that performs application-specific processing when anActionEventoccurs as a result of clicking a button or a hyperlink. The JavaServer Faces implementation automatically registers a defaultActionListenerto handle theAction Event.The processing an
Actionobject performs occurs in itsinvokemethod, which returns a logical outcome as a result of the processing. For example, theinvokemethod can return "failure" after checking if a password a user enters does not match the password on file.This outcome is returned to the default
NavigationHandlerby way of the defaultActionListenerimplementation. TheNavigationHandlerselects the page to be accessed next by matching the outcome against those defined in a set of navigation rules specified in the application configuration file.As the section Using an Action Object With a Navigation Rule explains, the component that generated the
ActionEventmaps to theActionobject with itsactionRefproperty. This property references a bean property that returns theActionobject.It is common practice to include the bean property and the
Actionimplementation to which it refers within the same bean class. Additionally, this bean class should represent the model data for the entire form from which theActionEventoriginated. This is so that theActionobject'sinvokemethod has access to the form data and the bean's methods.To illustrate how convenient it is to combine the form data and the
Actionobject, consider the situation in which a user uses a form to log in to a Web site. This form's data is represented by a bean calledLogonForm, which is configured in the application configuration file:<managed-bean> <managed-bean-name>logonForm</managed-bean-name> <managed-bean-class>foo.LogonForm</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean>This declaration creates the
LogonFormbean in request scope for each individual request if the bean is not already in request scope. For more information on creating beans, see Creating Model Objects.To logon, the user enters her username and password in the form. The following tags from the
login.jsppage accept the username and password input:<h:input_text id="username" size="16" valueRef="logonForm.username" /> <h:input_secret id="password" size="16" valueRef="logonForm.password"/>The
valueRefproperties of theseUIInputcomponents refer to theLogonFormbean properties. The data for these properties are updated when the user enters the username and password and submits the form by clicking the SUBMIT button. The button is rendered with thiscommand_buttontag:The
actionRefproperty refers to thegetLogonmethod of theLoginFormbean:This method returns an
Action(implemented here as an anonymous inner class), whoseinvokemethod returns an outcome. This outcome is determined by the processing performed in the bean'slogonmethod:protected String logon() { // If the username is not found in the database, or the password does not match that stored for the username: -Add an error message to the FacesContext -Return null to reload the current page. // else if the username and password are correct: -Save the username in the current session -Return the outcome, "success" }The
logonmethod must access the username and password that is stored in the username and password bean properties so that it can check them against the username and password stored in the database.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.