|
Backing Bean Management
Another critical function of web applications is proper management of
resources. This includes separating the definition of UI component objects
from objects that perform application-specific processing and hold data. It
also includes storing and managing these object instances in the proper scope.
A typical JavaServer Faces application includes one or more backing beans,
which are JavaBeans components (see
JavaBeans
Components) associated with UI components used in a page. A backing bean
defines UI component properties, each of which is bound to either a
component's value or a component instance. A backing bean can also define
methods that perform functions associated with a component, including
validation, event handling, and navigation processing.
To bind UI component values and instances to backing bean properties or to
reference backing bean methods from UI component tags, page authors use the
JavaServer Faces expression language (EL) syntax. This syntax uses the
delimiters #{}. A JavaServer Faces expression can be a value-binding
expression (for binding UI components or their values to external data
sources) or a method-binding expression (for referencing backing bean
methods). It can also accept mixed literals and the evaluation syntax and
operators of the JSP 2.0 expression language (see
Expression
Language).
To illustrate a value-binding expression and a method-binding expression,
let's suppose that the userNo tag of the guessNumber application referenced a method
that performed the validation of user input rather than using LongRangeValidator:
This tag binds the userNo component's value to the UserNumberBean.userNumber backing bean property.
It also refers to the UserNumberBean.validate method, which performs validation of the
component's local value, which is whatever the user enters into the field
corresponding to this tag.
The property bound to the component's value must be of a type supported by the
component. For example, the userNumber property returns an Integer, which is one of
the types that a UIInput component supports, as shown in
Developing
the Beans.
In addition to the validator attribute, tags representing a UIInput can also use a
valueChangeListener attribute to refer to a method that responds to ValueChangeEvents, which a UIInput
component can fire.
A tag representing a component that implements ActionSource can refer to backing bean
methods using actionListener and action attributes. The actionListener attribute refers to a
method that handles an action event. The action attribute refers to a method
that performs some processing associated with navigation and returns a logical
outcome, which the navigation system uses to determine which page to display
next.
A tag can also bind a component instance to a backing bean property. It does
this by referencing the property from the binding attribute:
The property referenced from the binding attribute must accept and return the
same component type as the component instance to which it's bound. Here is an
example property that can be bound to the component represented by the
preceding example inputText tag:
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 its model value, which is
updated with the local value during the update model values phase of the life
cycle.
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.
Backing beans are created and stored with the application using the managed
bean creation facility, which is configured in the application configuration
resource file, as shown in
Adding
Managed Bean Declarations. When the application starts up, it processes
this file, making the beans available to the application and instantiating
them when the component tags reference them.
In addition to referencing bean properties using value and binding attributes,
you can reference bean properties (as well as methods and resource bundles)
from a custom component attribute by creating a ValueBinding instance for it. See
Creating
the Component Tag Handler and
Enabling
Value-Binding of Component Properties for more information on enabling
your component's attributes to support value binding.
For more information on configuring beans using the managed bean creation
Facility, see
Configuring
Beans.
For more information on writing the beans and their properties, see
Writing
Component Properties.
For more information on binding component instances or data to properties, see
Binding
Component Values and Instances to External Data Sources.
For information on referencing backing bean methods from component tags, see
Referencing
a Backing Bean Method.
|