JBoss.orgCommunity Documentation

Chapter 6. Managed Beans

6.1. Code Generation for Managed Beans
6.2. Add Existing Java Beans to a JSF Configuration File

JSF Tools provides a number of useful features when working with managed beans, such as:

This guide will look at each of these features in more detail.

To begin, create a new managed bean in JSF configuration file editor using the Tree view.


Note:

When you define a new managed bean, make sure that Generate Source Code option is checked as shown in the figure below.


After the Java class has been generated you can open it for additional editing. There are two ways to open a Java class:

  • Click on the Managed-Bean-Class link in the editor.

  • Right click the managed bean and select the Open Declaration option.


The generated Java source should look as follows:


You can also generate source code for properties, also includes getter and setter methods. Right click on the bean and select NewProperty. You will then see the Add Property dialog.


When the form is open make sure that all the check boxes are selected:

  • Add Java property

  • Generate Getter

  • Generate Setter


Once the generation is complete, you can open the file and see the newly added property with accompanying "get" and "set" methods:


This covers the options available when creating a new Managed Bean. The next section will show you how to add an existing Bean into a JSF configuration file.

If you already have a Java bean you can easily add it to a JSF configuration file.

You should start the same way you create a new managed bean. Use the Browse... button to add your existing Java class.


Once the class is set, its Name will be set as well. But you can easily substitute it for the other one. Notice that Generate Source Code option is not available as the Java class already exists.

After adding your class the Next button will be activated. When you click it you will be presented with the Managed Properties dialog where all corresponding properties are displayed. Checking the appropriate ones will add them into your JSF Configuration File.


If you don't want to add any, just click the Finish button.

The steps above have demonstrated how you can add an existing Bean to the JSF configuration file, i.e. faces-config.xml. The next chapter will demonstrate how to organize and register other kinds of artifacts.