Monday, August 24, 2015

Access Referenced Managed Bean in JSF

Following code can be used to reference one manged bean from another.

@ManagedBean(name="usingBean")
@RequestScoped
public class UsingBean 
{
    @ManagedProperty(value="#{neededBean}")
    private NeededBean neededBean;

    public NeededBean getNeededBean()
    {
    return neededBean;
    }

    public void setNeededBean(NeededBean neededBean)
    {
    this.neededBean = neededBean;
    }
}

Now, you can use the following methods to call referenced managed bean property in JAVA class.

ELContext elContext = FacesContext.getCurrentInstance().getELContext();
NeededBean neededBean 
    = (NeededBean) FacesContext.getCurrentInstance().getApplication()
    .getELResolver().getValue(elContext, null, "neededBean");

---------------------------------- or --------------------------------------------

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


Reference site :- https://myfaces.apache.org/wiki/core/user-guide/jsf-and-myfaces-howtos/backend/accessing-one-managed-bean-from-another.html


No comments:

Post a Comment