Viewing page code for a Faces JSP file

Each Faces JSP file that you create has a corresponding file that contains its Java™ page code. The page code file includes such information as the Java classes that are referenced, data that is referenced when you drag records to the page, binding information, and any code scripted in the Quick Edit view for button actions or value changed events.

Note: A page code file is required if you want to add a relational record or relational record list to a Faces JSP. If you have page code suppression turned on when adding a relational record or relational record list, you are prompted to allow a page code file to be generated.

When you create a Faces JSP file, an empty Java class with the same name is generated in the pagecode package. For example if you create a Faces JSP named CustomerList.jsp, the class pagecode.CustomerList is created. This class is registered as a request scope managed bean in the Faces configuration file, with the name of the Java class prefixed with pc_, so the CustomerList class is registered as the managed bean pc_CustomerList. This managed bean is used for the root of most of the value and method binding expressions in the Faces JSP.

Page code managed beans are repositories for convenience methods for accessing relevant data from the page, such as UI components, other managed beans, and action logic. These methods are usually coupled with private references to the data, which are lazy initialized by the accessor methods.

To view the contents of the Java page code file for a Faces JSP page:

  1. Right-click within a blank area of a Faces JSP page in Design mode or Source mode.

  2. Click

    Edit Page Code.

By default, a page code file is generated for each Faces JSP, but you can change this at any time in the Preferences settings. You also have the options to delete page code file information when you delete a Faces JSP and to generate fields and getters in the page code file for all Faces components.

To configure the JSF page code options:

  1. Click

    Window | Preferences.

  2. Expand

    Web Tools and click

    Faces.

When you create the first JSF file in a Web project, the parent class for all the generated page code, pagecode.PageCodeBase, is generated into the project. This is a good location to add common functions that you want to use in all of your page code.

The parent for all page code comes with a number of convenience methods to be used in the action logic. These methods assist in finding components in the UI tree, manual navigation, and logging, among other functions.

 

Example of UI components in page code

For example, drag a

Button - Command onto a Faces JSP page, change its ID to selectButton, and edit its command event so that the JSP source is:

<hx:commandExButton type="submit" value="Submit" id="selectButton"
     action="#{pc_CustomerSelect.doSelectButtonAction}">
</hx:commandExButton>

The page code managed bean is automatically updated with an instance of the UI component underlying this tag, a convenience method for initializing and accessing that component, and the method implementing the command event:

protected HtmlCommandExButton selectButton;
protected HtmlCommandExButton getSelectButton()
     if (selectButton ==null) {
          selectButton = (HtmlCommandExButton)
               findComponentInRoot("selectButton");
     }
     return selectButton;
}
public String doSelectButtonAction() {
     //Type Java code to handle command event here
     //Note, this code must retrun an object of type String (or null)
     return "select";
}

 

Example of managed beans in page code

When adding a managed bean reference to a page using the Page Data view, field and accessor methods to access the managed bean are generated into the page code. If you add the managed bean customerData to the Faces JSP, the following code is generated:

protected CustomerData customerData;
/**
 * @author Rational
 */
public CustomerData getCustomerData() {
     if (customerData == null) {
          customerData = new CustomerData();
          customerData = 
          (CustomerData) getFacesContext()
               .getApplication().createValueBinding(
               "#{customerData}").getValue(
               getFacesContext());
     }
     return customerData;
}
public void setCustomerData(CustomerData customerData) {
     this.customerData = customerData;
}

If you subsequently bind a UI component to this managed bean, the bean is accessed as a property of the page code bean. Examine this input field, which is bound to a property of the customerData managed bean:

<h:inputText styleClass="inputText" id="text1"
     value="#{pc_CustomerSelect.customerData.firstName}"/>