HTML and CSS Reference
In-Depth Information
Initializing Managed Beans
JSF managed beans can be initialized from both the faces configuration file or using annotation. Listing 2-3
shows how to initialize the name property of the User managed bean with value "anonymous" from the faces
configuration file.
Listing 2-3. Initializing the Name Property in the faces-config.xml
<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-property>
<property-name>name</property-name>
<value>anonymous</value>
</managed-property>
</managed-bean>
The <managed-property> element can be used for initializing the managed bean property. It has mainly two
sub-elements:
<property-name> element, which includes the name of the managed bean property.
<value> element, which includes the initial value of the managed bean property.
Another way to initialize the managed bean properties is to use annotations. Listing 2-4 shows how to initialize
the name property of the User managed bean with value "anonymous" using the @ManagedProperty annotation.
Listing 2-4. Initializing the Name Property Using the @ManagedProperty Annotation
@ManagedBean
@SessionScoped
public class User implements Serializable {
@ManagedProperty(value="anonymous")
private String name;
private String password;
// The setters and the getters ...
}
Using annotations, the @ManagedProperty annotation is used for initializing the name property of the User
managed bean with the value "anonymous" using the value attribute.
It is important to note that the JSF managed beans can work perfectly with the different Java EE annotations.
There are two Java EE annotations that are related to the JSF managed beans life cycle and can be used for initializing
and de-initializing the managed beans:
@PostConstruct
Listing 2-5 shows an example of the @PostConstruct and the @PostDestroy annotations in the User managed bean.
@PreDestroy
 
Search WWH ::




Custom Search