Oracle® SOA Suite Developer's Guide 10g (10.1.3.1.0) Part Number B28764-01 |
|
|
View PDF |
In JSF, backing beans are JavaBeans used mainly to provide UI logic and to manage data between the web tier and the business tier of the application (similar to a data transfer object). Typically you have one backing bean per JSF page. The backing bean contains the logic and properties for the UI components used on the page. For example, to programmatically change a UI component as a result of some user activity or to invoke a service method, you provide the necessary code in the page's backing bean and bind the component to the corresponding property or method in the bean.
For example, in the SOADEMO-CLIENT application, the Register
backing bean contains a method to which the Register command component is bound. When the user clicks the Register button, the data entered on the page is submitted, and this method is invoked. Because the data from the submitted fields is bound to instances of the components using the backing bean, the method can access those values to create a Customer
object.
For a backing bean to be available when the application starts, you register it as a managed bean with a name and scope in faces-config.xml
. At runtime, whenever the bean is referenced on a page through a JSF EL value or method binding expression, the JSF implementation automatically instantiates the bean, populates it with any declared, default values, and places it in the managed bean scope as defined in faces-config.xml
.
The Overview mode of the JSF Configuration Editor lets you create and configure a backing bean declaratively.
To create and configure a backing bean as a managed bean:
In the Application Navigator, double-click faces-config.xml
to open it in the default mode of the JSF Configuration Editor.
At the bottom of the editor, select the Overview tab to switch to the Overview mode, if necessary.
In the element list on the left, select Managed Beans.
Click New to open the Create Managed Bean dialog.
In the dialog, specify the following for a managed bean:
Name: Enter a unique identifier for the managed bean (e.g., SOA_Demo_Login
). This identifier determines how the bean will be referred to within the application using EL expressions, instead of using the bean's fully qualified class name.
Class: Enter the fully qualified class name (e.g., oracle.srdemo.view.backing.SoaDemoLogin
). This is the JavaBean that contains the properties that hold the data for the UI components used on the page, along with the corresponding accessor methods and any other methods (such as navigation or validation). This can be an existing or a new class.
Scope: This determines the scope within which the bean is stored. The valid scope values are:
application: The bean is available for the duration of the web application. This is helpful for global beans such as LDAP directories.
session: The bean is available to the client throughout the client's session.
request: The bean is available from the time it is instantiated until a response is sent back to the client. This is usually the life of the current page. Backing beans for pages usually use this scope.
none: The bean is instantiated each time it is referenced.
Select the Generate Class If It Does Not Exist checkbox to let JDeveloper create the Java class for you. If you've already created the Java class, don't select this checkbox.
Note: At this point, you haven't defined a strict relationship between the JSF page and the backing bean. You've simply configured a backing bean infaces-config.xml , which you can now reference via JSF EL expressions on a page. |
If you select the Generate Class If It Does Not Exist checkbox, JDeveloper creates a new Java class using the fully qualified class name set as the value of Class. The new file appears within the Application Sources node of the ViewController
project in the Application Navigator, as illustrated in Figure 9-8.
To edit the backing bean class, double-click the file in the Application Navigator (for example, SoaDemoLogin.java
) to open it in the source editor. If it's a new class, you would see something similar to Example 9-2.
Example 9-2 Empty Java Class Created by JDeveloper
package oracle.soademo.view.backing; public class SoaDemoLogin { public SoaDemoLogin() { } }
In faces-config.xml
, JDeveloper adds the backing bean configuration using the <managed-bean>
element, as shown in Example 9-3.
Example 9-3 Registering a Managed Bean in the faces-config.xml File
<faces-config xmlns="http://java.sun.com/JSF/Configuration"> ... <!-- Page backing beans typically use request scope--> <managed-bean> <managed-bean-name>SOA_Demo_Login</managed-bean-name> <managed-bean-class>oracle.srdemo.view.backing.SoaDemoLogin</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> ... </faces-config>
JDeveloper has a feature that lets you automatically bind a UI component instance on a JSF page to a backing bean property. When you turn on the Auto Bind feature for a page, for any UI component that you insert into the page, JDeveloper automatically adds property code in the page's backing bean, and binds the component's binding
attribute to the corresponding property in the backing bean. If your backing bean doesn't have to modify the attributes of UI components on a page programmatically, you don't need to use the automatic component binding feature.
You can decide to use automatic component binding when creating a JSF page, as shown in Step 7 in Section 9.3.1, "How to Create a JSF Web Page". You can also manually turn on automatic component binding for an existing page.
To turn on automatic component binding for an existing JSF page:
Open the JSF page in the visual editor. Select Design at the bottom of the editor window.
Choose Design > Page Properties to display the Page Properties dialog.
Select Component Binding.
Select Auto Bind.
Select a managed bean from the dropdown list or click New to configure a new managed bean for the page.
Note: By turning on automatic component binding in a JSF page, you are defining a strict relationship between a page and a backing bean in JDeveloper. |
If the Auto Bind feature is turned on for a JSF page, you'll see a special comment line near the end of the page:
...
</f:view>
<!--oracle-jdev-comment:auto-binding-backing-bean-name:backing_soaDemoPage-->
</jsp:root>
In faces-config.xml
, a similar comment line is inserted at the end of the page's backing bean configuration:
<managed-bean>
<managed-bean-name>backing_soaDemoPage</managed-bean-name>
<managed-bean-class>oracle.srdemo.view.backing.soaDemoPage</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<!--oracle-jdev-comment:managed-bean-jsp-link:1soaDemoPage.jsp-->
</managed-bean>
When you turn on the Auto Bind feature for a page, JDeveloper does the following for you every time you add a UI component to the page:
Adds a property and property accessor methods for the component in the backing bean. For example, the next code snippet shows the code added for an inputText
and a commandButton
component:
private CoreInputText inputText1; private CoreCommandButton commandButton1; public void setInputText1(CoreInputText inputText1) { this.inputText1 = inputText1; } public CoreInputText getInputText1() { return inputText1; } public void setCommandButton1(CoreCommandButton commandButton1) { this.commandButton1 = commandButton1; } public CoreCommandButton getCommandButton1() { return commandButton1; }
Binds the component to the corresponding bean property using an EL expression as the value for the binding
attribute, as shown in this code snippet:
<af:inputText binding="#{backing_soaDemoPage.inputText1}" <af:commandButton binding="#{backing_soaDemoPage.commandButton1}"
When you turn off the Auto Bind feature for a page, JDeveloper removes the special comments from the JSF page and faces-config.xml
. The binding
EL expressions on the page and the associated backing bean code are not deleted.
Tip: When Auto Bind is turned on and you delete a UI component from a page, JDeveloper automatically removes the corresponding property and accessor methods from the page's backing bean. |