Saturday, June 2, 2012

Access managed beans in Java using EL


If a managed bean method requires access to an instance of another managed bean then one option is to
reference the managed bean in a managed bean property. Another option is to reference the managed
bean instance using Expression Language, as shown below


        FacesContext fctx = FacesContext.getCurrentInstance();
        ELContext elctx = fctx.getELContext();
        Application jsfApp = fctx.getApplication();
        ExpressionFactory exprFactory = jsfApp.getExpressionFactory();
        ValueExpression valueExpr = exprFactory.createValueExpression(
                                    elctx,
                                  "#{EmployeesBean}",
                                    Object.class);
        EmployeesBean employees = (EmployeesBean)  valueExpr  .getValue(elctx);