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:
  1. 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 New > JavaBean.
  2. 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).)
  3. Type a name for the JavaBean, for example myBirthDate.
  4. Specify a class for the JavaBean, for example: java.util.Date. You can also click Search to search for a class.
  5. Do one of the following:
    1. 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:

      Faces Managed Beans in the Page Data view

      <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>
    2. 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.
  6. Select Add input/output controls to display the JavaBean on the Web page to create a form for displaying the data from the JavaBean.
  7. Click Initialize Properties to display the Initialize JavaBean Properties dialog. Use this dialog to add properties that you want initialized.
    1. Click Add and then click within the Name field to display a list of available names or properties.
    2. Select a name from the list.
    3. 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.
    4. 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.)
    5. 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.
    6. Click OK when you are finished adding properties.
  8. Click Next. The Configure Data Controls panel displays.
  9. Choose whether to create controls for Displaying data (read-only) or to create controls for Inputting data.
  10. Check Invoking a method, then choose a method from the list or click Add to display the methods for the class you chose.
  11. 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.

  12. Use the up and down arrows on the right to change the order of the fields.
  13. Click Options if you want to specify in more detail how the fields are displayed.
  14. Click Next for more data control configuration options.
  15. Select Create controls for displaying the results if you want input results to display.
  16. 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.
Related concepts
Working with data in Faces applications
Related tasks
Creating a connection to a relational database
Adding a relational record list to a Faces JSP file
Adding relational record data to a Faces JSP page
Adding Domino Notes and Views to a Faces JSP file
Passing parameters between JSP files

Feedback