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, 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.

You can add managed beans to your JSF configuration file by either editing the XML in the file manually, or using an overview editor for configuration files, which provides dialogs and browse features for finding class file references for your beans. In the example, you will use the overview editor and Create Managed Bean dialog to add a managed bean for the Java class you have already created.

Create Managed Bean dialog

When adding managed beans to faces-config.xml, if you have not already created the bean class, you can use the dialog to ask for a bean file to be created for you.

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 editor for faces-config.xml:

                               
<managed-bean>
  <managed-bean-name>personData</managed-bean-name>
  <managed-bean-class>project1.PersonInfo</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
                            

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.

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

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