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.
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.
It lasts for the duration of the web application.
It is shared among all requests and all sessions.
@Named(bean-name) @SessionScope
Session scope is the most common scope. Beginning web application developers should only use session scope.
It lasts during repeated connections by the same client.
Session tracking is usually done with cookies or through URL rewriting and session IDs. The mechanism is transparent to web application developers.
@Named(bean-name) @RequestScope
Request scope is usually an optimization for reducing the resources used by a web application.
It is short-lived; a new instance created with each request.
It is not appropriate if data is to persist beyond the request.
@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.
x + y
.
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
ProblemBean.java
,
QuizBean.java
, and
index.xhtml
.
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:
Adjectives like "current" do not make good property names. A noun such as "problem" is much better. The name of a property should suggest what the property is, not just one of its characteristics.
A characteristic of a property may be useful as part of its name. For example, the name "currentProblem" may be desirable when the application needs to display both the correct answer to the previous problem and the question for the current problem.
The name "answer" is ambiguous. It could have two possible meanings:
Here, the second meaning is intended. A name like "userAnswer" or "response" would clarify the intention.
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.