Skip Headers
Oracle® Application Development Framework Developer's Guide
10g Release 3 (10.1.3)
B25386-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

10.2 Using a Managed Bean to Store Information

Often, pages require information from other pages in order to display correct information. Instead of setting this information directly on a page (for example, by setting the parameter value on the page's page definition file), which essentially hardcodes the information, you can store this information on a managed bean. As long as the bean is stored in a scope that is accessible, any value for an attribute on the bean can be accessed using an EL expression.

For example, the SREdit page requires the value of the svrId attribute for the row selected by the user on the previous page. This value provides the parameter value for the findServiceRequestById(Integer) method used to display the form. Additionally, the method bound to the Cancel button needs to return an outcome allowing the user to return to the correct page (the SREdit page can be accessed from three different pages). The SRDemo application has a managed bean that holds this information, allowing the sending page to set the information, and the SREdit page to use the information in order to determine where to navigate for the Cancel action. This information is stored as a hash map in the bean.

Managed beans are Java classes that you register with the application using the faces-config.xml file. When the JSF application starts up, it parses this configuration file and the beans are made available and can be referenced in an EL expression, allowing access to the beans' contents. Whenever a managed bean is referenced, the Managed Bean Creation Facility instantiates the bean by calling the default constructor method on the bean. If any properties are also declared, they are populated with the declared default values.

10.2.1 How to Use a Managed Bean to Store Information

Using the JSF Configuration Editor in JDeveloper, you can create a managed bean and register it with the JSF application at the same time.

To create a managed bean:

  1. Open the faces-config.xml file. This file is stored in the <project_name>/WEB-INF directory.

  2. At the bottom of the window, select the Overview tab.

  3. In the element list on the left, select Managed Beans. Figure 10-1 shows the JSF Configuration Editor for the faces-config.xml file.

    Figure 10-1 The JSF Configuration Editor

    The JSF Configuration Editor shows all the managed beans
  4. Click the New button to open the Create Managed Bean dialog, as shown in Figure 10-2. Enter the name and fully qualified class path for the bean. Select a scope, select the Generate Java File checkbox, and click OK.

    Figure 10-2 The Create Managed Bean Dialog

    Fields to enter name, class, and scope

    Tip:

    If the managed bean will be used by multiple pages in the application, you should set the scope to Session. However, then the bean cannot contain any reference to the binding container, as the data on the binding object is on the request scope, and therefore cannot "live" beyond a request. For examples of when you may need to reference the binding container, see Section 10.5, "Overriding Declarative Methods".

  5. You can optionally use the arrow to the left of the Managed Properties bar to display the properties for the bean. Click New to create any properties. Press F1 for additional help with the configuration editor.


    Note:

    While you can declare managed properties using the configuration editor, the corresponding code is not generated on the Java class. You will need to add that code.

10.2.2 What Happens When You Create a Managed Bean

When you use the configuration editor to create a managed bean, and elect to generate the Java file, JDeveloper creates a stub class with the given name and a default constructor. Example 10-1 shows the code added to the MyBean class stored in the view package.

Example 10-1 Generated Code for a Managed Bean

package view;
 
public class MyBean {
    public MyBean() {
    }
}

JDeveloper also adds a managed-bean element to the faces-config.xml file. This declaration allows you to easily access any logic on the bean using an EL expression that refers to the given name. Example 10-2 shows the managed-bean element created for the MyBean class.

Example 10-2 Managed Bean Configuration on the faces-config.xml File

<managed-bean>
  <managed-bean-name>my_bean</managed-bean-name>
  <managed-bean-class>view.MyBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

You now need to add the logic required by your pages and methods in your application. You can then refer to that logic using an EL expression that refers to the managed-bean-name given to the managed bean. For example, to access the myMethod() method on the bean, the EL expression would be:

#{my_bean.myMethod}

The following sections of this chapter provide examples of using the SRDemo application's userState managed bean (view.UserSystemState.java) to hold or get information. Please see those sections for more detailed examples of using a managed bean to hold information.