March 1, 2021
Hot Topics:

JBoss Tools 3 Developer Guide

  • By Anghel Leonard
  • Send Email »
  • More Articles »

The Tree View

Maybe the most important view of this editor is the Tree view, which is made of a set of nodes that put the configuration file in a graphical and easy-to-use manner. Just with a few clicks, we can create and modify the properties of the JSF components by skipping the annoying process of manually editing the configuration file. In this section you will see how to exploit this view for managing the most important components of a JSF application, such as managed beans, converters, and validators. The Tree view can be seen as shown in Figure 11.

Click here for larger image

Figure 11: The Tree View

Working with Managed Beans

A very powerful facility of the Tree view is the support for managed beans. Now, you can create, modify, and delete managed beans without touching the source code. Next, you will see how to create beans properties, how to create getter and setter methods, and how to obtain the generated source code in just a few steps.

As you have seen in the introduction of this article, you want to develop a registration form (register.jsp) with four fields representing name (java.lang.String), age (int), birth date (java.util.Date), and phone number (register.PhoneNumber) of the user. Each of these fields can be encapsulated as a property of a managed bean that can be developed by following these steps:

  1. Select the Managed Beans node from the left panel inside the Tree view.

  2. From the right panel (which is displaying the current managed beans status), click on the Add button. This will open the New Managed Bean window (as shown in Figure 12):

    Click here for larger image

    Figure 12: The New Managed Bean Window

  3. In this window, you have to specify the scope where the managed bean is active (in this case, the request scope), the class that represents the managed bean (for example, type register.PersonBean), and a bean's alias name used as a shortcut name (for example, type personBean). Because you want to allow JSF Tools to generate the managed bean source code, you mark as selected the Generate Source Code checkbox. Note that if you click on the Class link, you can configure internal aspects of the bean class (in this example, you will use the default settings).

  4. Clicking the Finish button of the New Managed Bean window will generate the managed bean, and it will display as shown in Figure 13.

    Click here for larger image

    Figure 13: The New Managed Bean Window Will Generate the Managed Bean

Now that you have created the managed bean stub, it is time to create the bean's properties. For doing this, you can follow these steps:

  1. Click on the Add button in Properties panel. This will open the Add Property window (as shown in Figure 14):

    Click here for larger image

    Figure 14: The Add Property Window

  2. In this window, you have to specify the new property name (for example, for mapping the username—Your Name field of your registration form—type personName in the Property-Name field), the property type (in this example, the username is a string), the value kind, and the default value (fill up the Value field only if you want to use a different default value). Note that you have marked as selected the three checkboxes for adding the new property to your bean and to generate the corresponding getter/setter methods. At the end, click on the Finish button for generating the new property. Figure 15 will appear.

    Click here for larger image

    Figure 15: Generating the New Property

    For primitive types (including strings), the value kind is recommended to be set as value. For objects, it is recommended to set it as null-value. List-entries and map-entries values are also supported.

  3. Now, if you repeat the above two steps, you can map the user age (Your Age field), user birth date (Your Birth Date field), and user phone number (Your Phone Number field) in the same way. Figure 16, Figure 17, and Figure 18 represent the settings that should be made in the Add Property window for mapping these fields to bean properties:

    Click here for larger image

    Figure 16: The Add Property Window for Mapping User Age

    Click here for larger image

    Figure 17: The Add Property Window for Mapping User Birth Date

    Click here for larger image

    Figure 18: The Add Property Window for Mapping User Phone Number

An interesting aspect is represented by the Your Phone Number field, because this field is mapped to another managed bean named register.PhoneNumber. This managed bean has three properties used for extracting from the inserted phone number, the country code (countryCode property), area code (areaCode property), prefix number (prefixNumber property), and a property for keeping the entire phone number (allNumber property). These four properties are all strings (java.lang.String). Now, use what you have learned so far to create this managed bean with JSF Tools (for an alias name, use the phoneNumber text).

In the end, you should have the two managed beans as shown in Figure 19, Figure 20, and Figure 21.

Click here for larger image

Figure 19: Faces Config Editor View of Two Managed Beans

Click here for larger image

Figure 20: Faces Config Editor View of personBean

Click here for larger image

Figure 21: Faces Config Editor View of phoneNumber

Finally, save the project state by selecting the Save option from the File main menu.

If you want to see the generated code source for your beans, you can double-click the Managed-Bean-Class link that appears in the right panel of the Tree view when you select a bean from the left panel of the same view (as shown in Figure 22).

Click here for larger image

Figure 22: The Generated Code Source for Your Beans

You can also open a bean from the Package Explorer view by expanding the project node and double-clicking on the bean's name.

In addition, JSF Tools allows you to add existing Java Beans to the configuration file. This is a very useful facility because it allows you to import existing Java Beans so that you don't have to recreate them from the start. For this, you select the Managed Beans node (in Tree view) and click the Add button in the right panel. In the New Managed Bean wizard, you browse to the existing Java Bean, click the Next button (adding an exiting bean will deactivate the Generate Source Code checkbox and activate the Next button), select the properties to be imported (from the Managed Properties wizard), and click on the Finish button.

Page 4 of 5

This article was originally published on June 4, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date