Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  Java Servlet Technology

5.  JavaServer Faces Technology

JavaServer Faces Technology User Interface

JavaServer Faces Technology Benefits

What Is a JavaServer Faces Application?

User Interface Component Model

User Interface Component Classes

Component Rendering Model

Conversion Model

Event and Listener Model

Validation Model

Navigation Model

The Life Cycle of a JavaServer Faces Page

Restore View Phase

Apply Request Values Phase

Process Validations Phase

Update Model Values Phase

Invoke Application Phase

Render Response Phase

Further Information about JavaServer Faces Technology

6.  Introduction to Facelets

7.  Using JavaServer Faces Technology in Web Pages

8.  Developing with JavaServer Faces Technology

9.  Configuring JavaServer Faces Applications

Part III Web Services

10.  Introduction to Web Services

11.  Building Web Services with JAX-WS

12.  Building RESTful Web Services with JAX-RS and Jersey

Part IV Enterprise Beans

13.  Enterprise Beans

14.  Getting Started with Enterprise Beans

15.  Running the Enterprise Bean Examples

Part V Persistence

16.  Introduction to the Java Persistence API

17.  Running the Persistence Examples

18.  The Java Persistence Query Language

Part VI Security

19.  Introduction to Security in the Java EE Platform

20.  Using Java EE Security

21.  Securing Java EE Applications

22.  Securing Web Applications

Part VII Java EE Supporting Technologies

23.  Introduction to Java EE Supporting Technologies

24.  Transactions

25.  Resource Connections

Index

 

Backing Beans

A typical JavaServer Faces application includes one or more backing beans, each of which is a JavaServer Faces managed bean that is associated with the UI components used in a particular page. Managed beans are JavaBeans components that you can configure using the managed bean facility, which is described in Configuring Beans. This section introduces the basic concepts on creating, configuring, and using backing beans in an application.

Creating a Backing Bean Class

In addition to defining a no-arg constructor, as all JavaBeans components must do, a backing bean class also defines a set of UI component properties and possibly a set of methods that perform functions for a component.

Each of the component properties can be bound to one of the following:

  • A component’s value

  • A component instance

  • A converter instance

  • A listener instance

  • A validator instance

The most common functions that backing bean methods perform include the following:

  • Validating a component’s data

  • Handling an event fired by a component

  • Performing processing to determine the next page to which the application must navigate

As with all JavaBeans components, a property consists of a private data field and a set of accessor methods, as shown by this code:

Integer userNumber = null;
...
public void setUserNumber(Integer user_number) {
    userNumber = user_number;
 }
public Integer getUserNumber() {
    return userNumber;
}
public String getResponse() {
    ...
}

Because backing beans follow JavaBeans component conventions, you can reference beans that you’ve already written from your JavaServer Faces pages.

When a bean property is bound to a component’s value, it can be any of the basic primitive and numeric types or any Java object type for which the application has access to an appropriate converter. For example, a property can be of type Date if the application has access to a converter that can convert the Date type to a String and back again. See Writing Bean Properties for information on which types are accepted by which component tags.

When a bean property is bound to a component instance, the property’s type must be the same as the component object. For example, if a UISelectBoolean is bound to the property, the property must accept and return a UISelectBoolean object.

Likewise, if the property is bound to a converter, validator, or listener instance, then the property must be of the appropriate converter, validator, or listener type.

For more information on writing beans and their properties, see Writing Bean Properties.

Configuring a Bean

JavaServer Faces technology supports a sophisticated managed bean creation facility, which allows application architects to do the following:

  • Configure simple beans and more complex trees of beans

  • Initialize bean properties with values

  • Place beans in a particular scope (available scopes: request, view, session, application)

  • Expose the beans to the unified EL so that page authors can access them

An application architect configures the beans in the application configuration resource file. To learn how to configure a managed bean, see Configuring Beans. The following example shows a sample faces-config.xml file:

<managed-bean>
    <managed-bean-name>UserNumberBean</managed-bean-name>
    <managed-bean-class>
        guessNumber.UserNumberBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>minimum</property-name>
        <property-class>long</property-class>
        <value>0</value>
    </managed-property>
    <managed-property>
        <property-name>maximum</property-name>
        <property-class>long</property-class>
        <value>10</value>
    </managed-property>
</managed-bean>

The JavaServer Faces implementation processes the <managed-bean-scope> element at the time of application startup. When a bean is first referenced from the page, it is instantiated.

In the above example, the bean is stored in a new session scope, if a session scope has not been created before. When the bean is created in session scope, it is available for all pages in the application (though in some cases, the bean may be instantiated but not stored in any scope).

A page author can then access the bean properties from the component tags on the page using the unified EL, as shown here:

<h:outputText value="#{UserNumberBean.minimum}"/>

The part of the expression before the period (.) matches the name defined by the managed-bean-name element. The part of the expression after the period (.) matches the name defined by the property-name element corresponding to the same managed-bean declaration.

Notice that the application configuration resource file does not configure the userNumber property. Any property that does not have a corresponding managed-property element will be initialized to the value of the instance variable, set by the constructor of the bean class. The next section explains more about using the unified EL to reference backing beans.

It is also possible to leverage the Annotations feature for managed beans to avoid configuring the managed-bean in application configuration resource file. For more information on using annotations, see Using Annotations.

For more information on configuring beans using the managed bean creation facility, see Configuring Beans.

Using the Unified EL to Reference Backing Beans

To bind UI component values and objects to backing bean properties or to reference backing bean methods from UI component tags, page authors use the unified expression language (EL) syntax defined by JSP 2.1. The following are some of the features this language offers:

  • Deferred evaluation of expressions

  • The ability to use a value expression to both read and write data

  • Method expressions

These features are all especially important for supporting the sophisticated UI component model offered by JavaServer Faces technology.

Deferred evaluation of expressions is important because the JavaServer Faces life cycle is split into separate phases so that component event handling, data conversion and validation, and data propagation to external objects are all performed in an orderly fashion. The implementation must be able to delay the evaluation of expressions until the proper phase of the life cycle has been reached. Therefore, its tag attributes always use deferred evaluation syntax, which is distinguished by the #{} delimiter. The Life Cycle of a JavaServer Faces Page describes the life cycle in detail.

In order to store data in external objects, almost all JavaServer Faces tag attributes use lvalue value expressions, which are expressions that allow both getting and setting data on external objects.

Finally, some component tag attributes accept method expressions that reference methods that handle component events, or validate or convert component data.

To illustrate a JavaServer Faces tag using the unified EL, let’s suppose that a tag of an application referenced a method to perform the validation of user input:

<h:inputText id="userNo"
     value="#{UserNumberBean.userNumber}"
     validator="#{UserNumberBean.validate}" />

This tag binds the userNo component’s value to the UserNumberBean.userNumber backing bean property using an lvalue expression. It uses a method expression to refer to the UserNumberBean.validate method, which performs validation of the component’s local value. The local value is whatever the user enters into the field corresponding to this tag. This method is invoked when the expression is evaluated, which is during the process validation phase of the life cycle.

Nearly all JavaServer Faces tag attributes accept value expressions. In addition to referencing bean properties, value expressions can also reference lists, maps, arrays, implicit objects, and resource bundles.

Another use of value expressions is binding a component instance to a backing bean property. A page author does this by referencing the property from the binding attribute:

<inputText binding="#{UserNumberBean.userNoComponent}" />

Those component tags that use method expressions are UIInput component tags and UICommand component tags. See sections Using Text Components and Using Command Components for Performing Actions and Navigation for more information on how these component tags use method expressions.

In addition to using expressions with the standard component tags, you can also configure your custom component properties to accept expressions by creating ValueExpression or MethodExpression instances for them.

To learn more about using expressions to bind to backing bean properties, see Binding Component Values and Instances to External Data Sources.

For information on referencing backing bean methods from component tags, see Referencing a Backing Bean Method.