tell me more icon JSF Managed Beans

While JSF allows you to bind a component in the user interface directly to any JavaBean, the best choice is to use JSF managed beans.

Managed beans are any JavaBeans used by the application that are registered in the JSF configuration file faces-config.xml. When the JSF application starts up, it parses this configuration file and the beans are made available. Whenever a managed bean is referenced (for example in an EL expression as a value for a component's tag attribute - called a value expression), 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.

To define a managed bean, you add an entry to the JSF configuration file faces-config.xml, giving a symbolic name that you will use to refer to the bean and identifying the class to be used to instantiate the bean. You use the symbolic name to identify the bean when you need to refer to the bean's properties and methods. Because you are using a symbolic name rather than referring to the bean directly, the presentation is kept separate from the application logic, which means that you can change the model without affecting the view.

At any time, you can add managed beans to the faces-config.xml file by either editing the XML in the file manually, or using an overview editor for configuration files, which provides creation dialogs and browse features for finding class file references for your beans.

Overview editor for JSF configuration file

In the example, you are creating the managed bean from the Page Properties dialog. When you click N ew to use the Create Managed Bean dialog to add a managed bean and generate the Java class for the bean, JDeveloper automatically updates the faces-config.xml file for you with the necessary configuration elements.

IDE, Page Properties dialog, Create Managed Bean

Managed beans are registered using the <managed-bean> element in the faces-config.xml file. After creating the managed bean, you should see the following code in the XML source editor for faces-config.xml:

                               
<managed-bean>
  <managed-bean-name>backing_login</managed-bean-name>
  <managed-bean-class>project1.backing.LoginInfo</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <!--oracle-jdev-comment:managed-bean-jsp-link:1Login.jsp-->
</managed-bean>
                            

The comment line <!--oracle-jdev-comment...--> tells you that the managed bean is the backing bean for the Login.jsp file.

The scope of a managed bean determines the scope within which the bean is stored. The following are the valid scopes for a bean:

  • 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.
  • none: The bean is instantiated each time it is referenced. This is helpful if the bean is referenced within another bean.

After creating the managed bean, in the source code of the Login.jsp page, you will see:

  • The comment line <%--oracle-jdev-comment...--%> for auto binding. For example:
                                       
    <%--oracle-jdev-comment:auto-binding-backing-bean-name:backing_login--%>
                                    
  • The binding and id code added to each UI component on the page. For example:
                                       
    <h:inputText binding="#{backing_login.inputText1}" id="inputText1"/>
                                    

JDeveloper also opens the backing bean file LoginInfo.java in the source editor, in which you will see the properties and accessor methods created for each UI component that you have on the page.

Copyright © 1997, 2009, Oracle. All rights reserved.

false ,,,,,,,,,,,,,,,