Accessing one Managed Bean from another in JSF

There is some scenarios where you need information stored in one managed beans for the business logic in some other beans.There are two ways for one managed bean to access another managed bean information.



1 . Using Dependency Injection in JSF2.0

We can use JSF 2 @ManagedProperty annotation for this purpose.In the managed bean add a @ManagedProperty annotation to the related property.






@ManagedBean(name="userBean")
@RequestScoped
public class UserBean 
{

@ManagedProperty(value="#{employeeBean}")
private EmployeeBean employeeBean;

public EmployeeBean getEmployeeBean()
{
return name;
}

public void setEmployeeBean(EmployeeBean employeeBean)
{
this.employeeBean = employeeBean;
}

// ....

}

In the above code we are accessing the EmployeeBean managed Bean property in UserBean managed class.

2 . By Using faces-config.xml

<managed-bean>
   <managed-bean-name>employeeBean</managed-bean-name>
   <managed-bean-class>com.jwt.EmployeeBean</managed-bean-class>
   <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 
 <managed-bean>
   <managed-bean-name>departmentBean</managed-bean-name>
   <managed-bean-class>com.jwt.DepartmentBean</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
   <managed-property>
     <property-name>name</property-name>
     <value>#{name}</value>
   </managed-property>
 </managed-bean>

Rules :

  • The current bean must have scope which is the same as or shorter than the required bean
  • The using bean must have a property-setter method which takes the required bean as a parameter.
  • The beans cannot have managed dependencies on each other.

In Managed Bean :

FacesContext facesContext = FacesContext.getCurrentInstance();
NeededBean neededBean
    = (NeededBean)facesContext.getApplication()
      .createValueBinding("#{neededBean}").getValue(facesContext);


Example :

Here is the example in which we are accessing method of one managed bean in another managed Bean.

BookBean.java

public class BookBean {
    public void readBook() {
        System.out.print("I am reading a book");
    }
}

StudentBean.java

public class StudentBean {
    public void readBookStudent() {
        FacesContext context = FacesContext.getCurrentInstance();
        Application application = context.getApplication();
        ValueBinding binding = application.createValueBinding("#{otherBean}");
        om.jwt.jsf.BookBean book = (com.jwt.jsf.BookBean)binding.getValue(context);
        book.readBook();
    }
}

faces-config.xml

<managed-bean>
        <managed-bean-name>studentBean</managed-bean-name>
        <managed-bean-class>com.jwt.jsf.StudentBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
<managed-bean>
        <managed-bean-name>otherBean</managed-bean-name>
        <managed-bean-class>com.jwt.jsf.BookBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

In the above code in StudentBean class we are accessing readBook() method of BookBean class:




comments powered by Disqus