JSF Managed Bean

JSF Managed Bean

1.     Bean class have Data Member and getter and setter

2.     A managed bean is a java bean class which contains data member and method (business logic).

3.     A managed bean managed by JSF frameworks

4.     A managed bean act as a model for JSP application

5.     A managed bean configured in faces-config.xml file or by Annotation

 

Managed Bean function performed

1.     Component Data Validation

2.     Component’s Event Handling

3.     Determine the next page which must to Navigated by JSF application

Example: Bean Class

public class Employee { 

private String empID;  

public String getEmpId() {  

return name;  

}  

public void setEmpId(String empId) {  

this.empId = empId;  

}   

   }  

 

 

 

 

 

 

Managed Bean Ways to Configure

1.     XML configuration :

<managed-bean>  

<managed-bean-name>employee</managed-bean-name>  

<managed-bean-class>Employee</managed-bean-class>  

<managed-bean-scope>session</managed-bean-scope>  

</managed-bean>  

 

2.     Annotation Based configure:

import javax.faces.bean.ManagedBean;  

import javax.faces.bean.RequestScoped; 

@ManagedBean    /*  ManagedBean annotation */

@RequestScoped  /*  Scope annotation  */

public class Employee { 

private String empID;  

public String getEmpId() {  

return name;  

}  

public void setEmpId(String empId) {  

this.empId = empId;  

}   

   }  

 

@ManagedBean:

1.    It will register a class as a resource in JSF framework

2.   This calls not need to configured in config resource file

 

@RequestScoped:

1.   It provide Score for managed bean

2.   It will allow you to store your bean in list of scopes

a.   Request (@RequestScoped): Bean Created when a request for this bean comes and destroyed once the completed the Response

b.   View (@ViewScoped): Bean Created when a request involving this bean comes and destroyed whenever view will be changed.

c.    Session (@SessionScoped): Bean Created when a request for this bean comes and destroyed when session terminates.

d.   Application (@ApplicationScoped): Bean Created when a request involving this bean comes and remains for whole duration till the application runs on web

e.    None (@NoneScoped): Bean scopes not defined in this application

f.     Custom (@CustomScoped):  Bean scopes is non standard, its value must be configured as java.util.map, custom score are use very rarely

 

Eagar & Lazy Managed Bean

Lazy

1.     By Default managed bean are Lazy

2.     Lazy managed Bean instantiate only when requested from the application

Eagar

1.     To enable Eagar managed bean @ManagedBean(eager=true

2.     @ManagedBean(eager=true)   Annotation need to apply on the Class level



 NEXT--> A SIMPLE EXAMPLE OF JSP APPLICATION

Comments

Popular posts from this blog

JunitTest

Log4j2 Setup