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
userNotag of theguessNumberapplication referenced a method that performed the validation of user input rather than usingLongRangeValidator:<h:inputText id="userNo" value="#{UserNumberBean.userNumber}" validator="#{UserNumberBean.validate}" />This tag binds the
userNocomponent's value to theUserNumberBean.userNumberbacking bean property. It also refers to theUserNumberBean.validatemethod, 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
userNumberproperty returns anInteger, which is one of the types that aUIInputcomponent supports, as shown in Developing the Beans.In addition to the
validatorattribute, tags representing aUIInputcan also use avalueChangeListenerattribute to refer to a method that responds toValueChangeEvents, which aUIInputcomponent can fire.A tag representing a component that implements
ActionSourcecan refer to backing bean methods usingactionListenerandactionattributes. TheactionListenerattribute refers to a method that handles an action event. Theactionattribute 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
bindingattribute:The property referenced from the
bindingattribute 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 exampleinputTexttag:UIInput userNoComponent = null; ... public void setUserNoComponent(UIInput userNoComponent) { this.userNoComponent = userNoComponent; } public UIInput getUserNoComponent() { return userNoComponent; }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:
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
renderedproperty 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
valueandbindingattributes, you can reference bean properties (as well as methods and resource bundles) from a custom component attribute by creating aValueBindinginstance 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.