16.5 Backing bean management
Typical JSF applications include one or more backing beans. These are JavaBean components associated with UI components used in a page. A backing bean holds UI component properties, each of which is bound to either a component's value or a component's instance. Backing beans can also define methods that perform functions associated with a component, including validation, event handling, and navigation processing.
The binding of component values and instances to backing beans is done through the use of JSF expression language (EL) syntax. This syntax uses the delimiters # { }. A JSF expression can be a value-binding expression or a method binding expression. It can also accept mixed literals and the evaluation syntax and operators of the JSP 2.0 expression language.
To illustrate value-binding expressions and method-binding expressions, take a look at Example 16-17, which defines the operation component of the calculator application.
Example 16-17 An UIInput component using both value-binding and method-binding expressions
<h:inputText id="operation" value="#{calculator.operation}" validator="#{calculator.validateOperator}" required="true"> </h:inputText>
![]()
This tag binds the operation component's value to the calculator.operation backing bean property. it also refers to the calculator.validateOperator method, which performs validation of the component's local value, which is whatever the user enters into the field corresponding to this tag.
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 String, 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, instead of a component's value. This is done by referencing the property from the binding attribute:
<inputText binding="#{calculator.operationComponent}"The property referenced from the binding attribute must accept and return the same component type as the component instance to which it is bound. Example 16-18 shows an example property the can be bound to the component represented by the preceding inputText tag.
Example 16-18 Binding a component instance to a backing bean property
UIInput operationComponent = null; ... public void setOperationComponent(UIInput operationComponent) { this.operationComponent = operationComponent; } public UIInput getOperationComponent() { return operationComponent;}![]()
These are the advantages of binding a component instance to a bean property:
![]()
The backing bean can programmatically modify component attributes, like the rendered attribute, that specify whether the component will be rendered or not.
![]()
The backing bean can instantiate components rather than let the page author do so. These are the advantages of binding a component's value to a bean property:
![]()
The page author has more control over the component attributes.
![]()
The backing bean has no dependencies on the JSF API, allowing a greater separation between the presentation layer and the model layer.
![]()
The JSF 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.
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 16.2.3, Developing the beans. When the application starts up, it processes this file, making the beans available to the application and instantiating them when they are first referenced by the component tags.
ibm.com/redbooks