Creating a JSF Form for Entity Data

See Also

You can use the Form from Entity dialog box to generate a JSF form that contains fields for all properties contained in an entity class. You must already have a JSF managed bean created to handle any user data associated with the form.

If you use this dialog without having an associated managed bean, you can enter a name for the managed bean in the dialog, and that name will be used in the page regardless of whether it is valid or not. You can then create a managed bean using the IDE's Managed Bean wizard, or if you use the JSF Pages from Entity Classes wizard, managed beans are generated for all selected entity classes.

To generate a JSF form from an entity class:

  1. Open any Facelets page in the editor.
  2. Make sure that you have declared the JSF tag libaries in the page's root element (typically the <html> tag), as shown below:
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
  3. Either type 'jsf' in the editor, press Ctrl-Space, then select the JSF Form from Entity option, or, in the Palette, click and drag the JSF Form from Entity component into the desired location in the editor and release the mouse button. The JSF Form from Entity dialog box opens.
  4. In the Entity Bean drop-down, select the entity class that you want to generate the form from. The IDE scans the project for all entity classes and makes them accessible from the drop-down list.
  5. In the Managed Bean Property field, select a property. The property you choose must return an instance of the entity bean you selected in the Entity Bean drop-down. (This is typically a getter method for the given property.)
  6. Optionally, select the 'Generate read only view' option to create a form that contains read-only fields. When this option is selected, the IDE applies <h:outputText> tags for form fields, whereas <h:inputText> tags are applied when the option is not selected.
  7. Click OK. The IDE generates code for your Facelets page. For example, a Customer entity class containing a customerId property is displayed in the following format:
    <f:view>
        <h:form>
            <h1><h:outputText value="Create/Edit"/></h1>
            <h:panelGrid columns="2">
                <h:outputLabel value="CustomerId:" for="customerId" />
                <h:inputText id="customerId" value="#{customerController.selected.customerId}" title="CustomerId" required="true" requiredMessage="The CustomerId field is required."/>
                ...
                [ Other fields added here. ]
                ...
            </h:panelGrid>
        </h:form>
    </f:view>
  8. Optionally, remove any of the entries for columns that you do not want to display in your Facelets page.

You can customize the template that specifies the generated code for the JSF Form from Entity dialog. To do so, click the Customize Template link in the dialog, and in the template file make any changes to better suit your work patterns. Save the file. The next time you use the dialog, the changes you made to the template will appear in the generated code.

See Also
Creating a JSF Data Table for Entity Data
Working with JSF Components in the Palette
Editing JSF Pages
About JavaServer Faces Framework Support

Legal Notices