Java Reference
In-Depth Information
The @ManagedBean ( javax.faces.bean.ManagedBean ) annotation in a class
automatically registers that class as a resource with the JavaServer Faces implementation.
Such a registered managed bean does not need managed-bean configuration entries in
the application configuration resource file.
An example of using the @ManagedBean annotation in a class is as follows:
@ManagedBean
@SessionScoped
public class DukesBday{
...
}
The above code snippet shows a bean that is managed by the JavaServer Faces imple-
mentation and is available for the length of the session. You do not need to configure the
managed bean instance in the faces-config.xml file. In effect, this is an alternative
to the application configuration resource file approach and reduces the task of configuring
managed beans.
You can also define the scope of the managed bean within the class file, as shown in the
above example. You can annotate beans with request, session, application, or view scope.
All classes will be scanned for annotations at startup unless the faces-config element
in the faces-config.xml file has the metadata-complete attribute set to true .
Annotations are also available for other artifacts, such as components, converters, valid-
ators, and renderers, to be used in place of application configuration resource file entries.
These are discussed, along with registration of custom listeners, custom validators, and
custom converters, in Chapter 6 , “ Creating Custom UI Components and Other Custom
Objects .
Using Managed Bean Scopes
You can use annotations to define the scope in which the bean will be stored. You can spe-
cify one of the following scopes for a bean class:
• Application ( @ApplicationScoped ): Application scope persists across all
users' interactions with a web application.
• Session ( @SessionScoped ): Session scope persists across multiple HTTP re-
quests in a web application.
• View ( @ViewScoped ): View scope persists during a user's interaction with a
single page (view) of a web application.
Search WWH ::




Custom Search