Creating Objects Automatically as They Are Needed

With the converters in place, Example 6-4 works great, but there’s still a bit of magic left. You may be asking yourself how the reportHandler variable gets its value; it’s actually done automatically by JSF when the variable is first needed. JSF creates and configures instances of classes defined in the JSF faces-config.xml file for variables that are referenced in a value expression. Let’s take a closer look at this feature.

Example 6-5 shows the reportHandler variable declaration in the faces-config.xml file for the sample application, minus details that are not relevant for the examples in this chapter.

Example 6-5. ReportHandler declaration in faces-config.xml
<faces-config>
  ...
  <managed-bean>
    <managed-bean-name>reportHandler</managed-bean-name>
    <managed-bean-class>
      com.mycompany.expense.ReportHandler
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    ...
  </managed-bean>
  ...
</faces-config>

The <managed-bean> element declares a variable that the application uses. The variable is mapped to a class that complies with the JavaBeans specification rules on naming conventions for accessor methods and has a public no-arguments constructor—hence, the bean reference in the element name.

All three subelements shown in Example 6-5 are mandatory: the <managed-bean-name> element declares the variable name used to refer to instances of the class; the <managed-bean-class> element declares the fully qualified class name; the ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.