O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required