Adding a Java Bean to a Faces JSP file
To work with dynamic data on your Web page, you need to define
a data source, such as a JavaBean. The JavaBeans™ you create can be used on
a single Faces JSP, or, you can create Faces Managed Beans that you
can reuse on any Faces JSP in your project. Once you create a Faces
Managed Bean, you can easily drag it from the Page Data view to reuse on other
Faces JSPs. All Faces Managed Beans for all Faces JSPs in a Web project display
in the Page Data view and can be shared among the pages in the project.
To create a new JavaBean in a Faces JSP file:
- Open the Data drawer on the palette and drag a JavaBean component
to the Faces JSP file. The Add Java™ Bean wizard opens.
Note: An alternate
way to create a JavaBean is to right-click within the Page Data view and select .
- Select Add new JavaBean to create and add
a JavaBean. (If you already have an existing, reusable JavaBean in your project
and want to use that one, select Add existing reusable JavaBean
(Faces Managed Bean).)
- Type a name for the JavaBean, for example myBirthDate.
- Specify a class for the JavaBean, for example: java.util.Date.
You can also click Search to search for a class.
- Do one of the following:
- Check Make this JavaBean reusable (Make this JavaBean
a Faces Managed Bean) if you want to make this JavaBean available
to other pages. If you check this option, you can enter a description for
the JavaBean and choose one of the following scopes:
- none: the life span is shorter than a request (example:
an object created on the fly that is not accessible as a scope variable, like
an instance variable).
- application: the life span of the application running
on the server session (example: a database connection).
- session: the life span of the browser accessing
the application (example: an end-user's shopping cart).
- request: the life span of a Web page request from
a browser (example: a set of detailed data based on a record in a list).
Note: Faces Managed Beans display in two places, the Page Data view under
the Faces Managed Beans node and in the faces-config.xml file:
<managed-bean>
<managed-bean-name>pc_myBirthDate</managed-bean-name>
<managed-bean-class>pagecode.myBirthDate</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
- If you do not want to make this JavaBean reusable, clear the
check box. The JavaBean you create is only represented by the getter and setter
methods in the page code file, for example:
public Date getMyBirthDate() {
if (myBirthDate == null) {
myBirthDate = new date();
}
return myBirthDate;
}
public void setMyBirthDate(Date myBirthDate) {
this.myBirthDate = myBirthDate;
}
However, you can always change the JavaBean to a Faces
Managed Bean by right-clicking the bean in the Page Data view and selecting Configure.
- Select Add input/output controls to display the JavaBean
on the Web page to create a form for displaying the data from
the JavaBean.
- Click Initialize Properties to display the Initialize
JavaBean Properties dialog. Use this dialog to add properties
that you want initialized.
- Click Add and then click within the Name field
to display a list of available names or properties.
- Select a name from the list.
- Click within the Value field. You can
enter a value directly or browse for a value by clicking the button to the
right of this field. The Select Page Data Object dialog
opens so that you can select a value.
- The Data Type field is filled in, depending
on the property name you chose in the Name field. (Depending
on the data type, an additional dialog might open in which you can add further
values.)
- Click Add to add another property, Remove to
delete the selected property, or Up and Down to
change the order in which the properties display.
- Click OK when you are finished adding
properties.
- Click Next. The Configure
Data Controls panel displays.
- Choose whether to create controls for Displaying data
(read-only) or to create controls for Inputting data.
- Check Invoking a method, then choose a method
from the list or click Add to display the methods for
the class you chose.
- In the Fields to display table, check all
the fields that you want to display.
Depending on the data
type of a given field, you might be able to select a Control Type from a list.
If the property is a list or array, a new button displays under the Control
Type column. Click this button to display a wizard page on which you can select
the control types for the elements of the list or array.
- Use the up and down arrows on the right to change the order of
the fields.
- Click Options if you want to specify in
more detail how the fields are displayed.
- Click Next for more data control configuration
options.
- Select Create controls for displaying the results if
you want input results to display.
- Click Finish.
If you selected Add input/output controls to display the
JavaBean on the Web page, the JavaBean displays on the Faces JSP
page. The newly-created JavaBean also displays in the Page Data view.