Access one Managed Bean from another in JSF 2.0 | Techartifact

Requirement- Access one Managed Bean from another in JSF

Solution- There are two ways-

Dependency Injection

We can use JSF 2 @ManagedProperty annotation

In the managed bean add a @ManagedProperty annotation to the related property

public class CurrentBean 

    private RequiredBean requiredBean;

    public RequiredBean getRequiredBean()
        return requiredBean;

    public void setRequiredBean(RequiredBean requiredBean)
        this.requiredBean= requiredBean;

    // ....


Usingthrough faces-config.xml



Following are the constraints:

-> 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.

Invoke a method from Managed Bean when JSPX Page Loads in ADF.

Requirment -invoke a method from Managed Bean when JSPX Page Loads in ADF.

Suppose you have some method in managed bean, which you want to call on page load.For that
Your managed bean should implement implements PagePhaseListener i.e

public class vinayManagedBean implements PagePhaseListener{ }

You should import “oracle.adf.controller.v2.lifecycle.PagePhaseListener;”
Now you will be calling method in beforePhase event of ADF page life cycle.In side the beforePhase ,pageLoad method will be called.You can write your custom method there.

public void beforePhase(PagePhaseEvent pagePhaseEvent) {
if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) {

public void afterPhase(PagePhaseEvent pagePhaseEvent) {
if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_RENDER_ID) {
// onPagePreRender();

public void onPageLoad() {
if (!AdfFacesContext.getCurrentInstance().isPostback()) {
// add your onPageLoad event here

// to set the View Criteria on the Iterator
doSomeOperation();  ///your custom method.

After all then right click on .jspx page and go to page definitions file .In your JSPX page definition make sure you add the ControllerClass that points to the managed bean that handles the onPageLoad event.

<pageDefinition xmlns="" version="" id="InspectionTaskHomePageDef"
                Package="oper.view.pageDefs" ControllerClass="vinayManagedBean">

By doint this whenever you page loads, the onPageLoad() method gets invoked which inturn invokes the v() method. You can even call multiple methods in onPageLoad() method.

Note:- It will not work in jsff.

getting binding of component from one managed bean to another

Today i got one of the requirment to get the binding of some component which is binding in another managed bean.
This is something tedious requirment i have.. Well in JSF 2.0 you can get very easily by using below code

        FacesContext context = FacesContext.getCurrentInstance();
        BEAN bean = (RegionNavigationListener)context.getApplication().evaluateExpressionGet(context, "#{BEAN}", BEAN.class);

You can write above code as below as well

FacesContext fctx = FacesContext.getCurrentInstance();
Application application = fctx.getApplication();
ExpressionFactory expressionFactory = application.getExpressionFactory();
ELContext context = fctx.getELContext();
ValueExpression createValueExpression = expressionFactory.createValueExpression(context, "#{pageFlowScope.PageFlowScopeBean}",PageFlowScopeClass.class);
PageFlowScopeClass pageFlowScopeInstance =(PageFlowScopeClass) createValueExpression.getValue(context);