HTML and CSS Reference
In-Depth Information
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
The User managed bean is a Java bean class with two setters and getters for the name and the password properties.
The @ManagedBean annotation is used for registering the User class as a JSF managed bean. The @ManagedBean
annotation has an optional name attribute that describes the name of the managed bean to be used from the JSF
expressions. In the User managed bean, the name attribute is omitted; this means that the managed bean name will be
the same as the class name with the first character in lowercase i.e., it will be used from the JSF expressions like #{user} .
A JSF managed bean must have a scope associated with it that controls its life span. The scope can be in:
@RequestScoped ), which means that the bean will be instantiated and will be
alive as long as the HTTP request is alive.
Request Scope (
@ViewScoped ), which means that the bean will be instantiated and will be alive as
long as the user is staying in the same view.
View Scope (
@SessionScoped ), which means that the bean will be instantiated and will be
alive as long as the user's HTTP session is alive.
Session Scope (
@ApplicationScoped ), which means that the bean will be instantiated and
will be alive throughout the lifetime of the application.
None scope (
Application Scope (
@NoneScoped ), which means that the bean will not be instantiated and will not be
stored in any scope as a standalone entity. The none scoped managed bean can be used and
instantiated by another managed bean; in this case, the none scoped bean will have the scope
of its caller managed bean which instantiates it (i.e., the none scoped managed beans will be
alive as long as their caller managed beans are alive). You will see an example of this case in
the “Managing managed beans dependency” section.
In the JSF 2.2, there is a new scope called “Flow Scope” ( @FlowScoped ). this flow will be illustrated in detail in
the next chapter.
Note
Adding to Annotations, Every JSF managed bean can also be registered in the faces-config.xml (the JSF
configuration file). Listing 2-2 shows how the User managed bean can be defined in the faces-config.xml file instead
of using annotations.
Listing 2-2. The User Managed Bean Definition in the faces-config.xml File
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>com.jsfprohtml5.firstapplication.model.User</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
 
 
Search WWH ::




Custom Search