A Java bean is just an object that observes naming conventions to define the properties of the object. JSF beans are Java beans with management support from the JSF framework.

A Java bean is just an object that observes naming conventions to define the properties of the object.

Bean properties are defined by writing getter and setter methods. For a property named myProperty the methods are named

Note that the first letter of the property name is capitalized in the getter and setter method names.

Getter methods have no parameters. Setter methods have a single parameter - the new value for the property. The return type of the getter and the type of the setter parameter define the type of the property. They should be the same.

Property Access

A Java bean can have read-write access, read-only access, or write-only access. The allowed access is determined by which of the access methods is defined:

Read-only properties are useful for properties that are either

The primary mechanism for handling data in a JSF application is a Java bean. Web pages in a JSF application can directly access data from JSF beans. They can indirectly access data from ordinary Java beans.

A JSF managed bean is defined in a Java class file that

The JSF managed beans are created and destroyed according to their scope. They can be accessed in the JSF pages using EL expressions.

Declaring a JSF managed bean means giving it a name and a scope. The name is used for accessing beans with EL expressions in XHTML files. Declaration is done with a Java annotation similar to the following.

@Named(bean-name)
@SessionScope

This annotation immediately precedes the class declaration for the bean. If the "(bean-name)" is omitted the name is derived from the class name.

Among the scopes that can be declared are:

This web presentation only intends to give a brief overview of when to use these scopes. Except for session scope mechanics of their use should be learned only as needed.

@Named(bean-name)
@ApplicationScope

Application scope is useful for keeping track of application usage statistics. For example, web application developers may want to track user errors to determine what parts of a web application need improved user interactions or better explanation.

@Named(bean-name)
@SessionScope

Session scope is the most common scope. Beginning web application developers should only use session scope.

@Named(bean-name)
@RequestScope

Request scope is usually an optimization for reducing the resources used by a web application.

@Named(bean-name)
@ViewScope

View scope is associated with individual JSF pages.

@Named(bean-name)
@ConversationScope

Conversation scope allows web application developers to define their own scopes. It has a mechanism that programmers can use to determine when the scope begins and when it ends.

Bean configuration can be done either with annotations in Java class files or with the file beans.xml. In earlier versions of JSF configuration with beans.xml was required for some purposes. With JSF 2.0, this file can be left empty (it is still required) and the configuration can be done in the Java class files as shown earlier in this presentation.

Bean properties and methods are accessed in JSF pages with Expression Language (EL) expressions. The legal syntax for EL expressions depends on how they are used.

EL expressions in a JSF page may invoke access JSF bean properties and methods. They may also indirectly access non-JSF bean properties and methods.

The simplest for of a property expression just names the bean and one of its properties using the following syntax.

#{bean-name.property-name}

In an rvalue context these simple expressions can be combined with some simple operators. This is not legal in an lvalue context.

Method expressions are useful for action attributes of <h:commandButton> tags. They have the following syntax.

#{bean-name.method-name(arguments)}

When used in tag attributes, String arguments should be enclosed in single quotes when the entire value expression is double quoted.

The numberquiz application can be run here. The application

This application uses many ProblemBean objects, one for each quiz question. For this reason, it does not make sense to use JSF bean management. Instead, the ProblemBean objects are accessed indirectly through the quiz bean of the application. To make this indirect access work the Java bean naming conventions must be observed.

ProblemBean objects have two read-write bean properties:

The QuizBean class defines a JSF managed bean named quizBean with session scope. The name "quizBean" is not specified explicitly; it is a default name derived from the class name. It has the following bean properties, which are not very well named:

There are two problems with naming of the quizBean properties:

Here, there are direct accesses to the quizBean score and answer properties.

The most interesting bean access is the following line:

      <p>#{quizBean.current.sequence}</p>

This line directly accesses the quizBean JSF-managed bean for its current property. The current property is a ProblemBean which is an ordinary Java bean. Its sequence property is accessed indirectly through the quizBean.