JSF Managed Bean
It is a pure Java class which contains set of properties and set of getter, setter methods.
Following are the common functions that managed bean methods perform:
It also works as model for JFS Framework.
JSF Managed Bean Example
You can use this bean by the following ways.
Configuring Managed Bean into XML file
This is an older approach to configure bean into xml file. In this approach, we have to create a xml file named faces-config.xml. JSF provides <managec-bean> tag to configure the bean.
In the above example, we are listing bean-name, bean-class and bean-scope. So, it can be accessible in the project.
Configuring Managed Bean using Annotations
The @ManagedBean annotation in a class automatically registers that class as a resource with the JavaServer Faces. Such a registered managed bean does not need managed-bean configuration entries in the application configuration resource file.
This is an alternative to the application configuration resource file approach and reduce the task of configuring managed beans.
The @RequestScoped annotation is used to provide scope for ManagedBean. You can use annotations to define the scope in which the bean will be stored.
You can use following scopes for a bean class:
Eager Managed Bean
Managed bean is lazy by default. It means, bean is instantiated only when a request is made from the application.
You can force a bean to be instantiated and placed in the application scope as soon as the application is started. You need to set eager attribute of the managed bean to true as shown in the following example: