Dharisi Suresh
Dharisi Suresh

Reputation: 150

How to call Managed bean method in using expression language

I am developing a utility class, using this we need to call a managed bean method which is defined in EL Expression. Is there any examples like how to invoke a managed bean method using EL expression.

Here I don't know the type of Managed bean. but I know EL expression. So I cannot type cast to specific managed bean.

The expression is: #{phaseListenerBean.compListener}

How can my code call the compListener method on phaseListenerBean? My Utility class. It is avaliable in a Jar file.

`public void beforePhase(PhaseEvent event) { if(PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())){ SystemListLoaderHelper.populateSelectOneValidValues();

        SystemListLoaderHelper.populateSelectManyCheckboxValidValues();
        FacesContext context = FacesContext.getCurrentInstance();
        ExpressionFactory factory =context.getApplication().getExpressionFactory();
        MethodExpression methodExpression = factory.createMethodExpression(
                context.getELContext(), "#{phaseListenerBean.callModuleSpecificServiceCalls}", 
                Void.class.getClass(),  null);
        methodExpression.invoke(context.getELContext(), null);

// callModuleSpecificServiceCalls(); } }`

Upvotes: 1

Views: 2475

Answers (1)

Arturo Volpe
Arturo Volpe

Reputation: 3637

You can try call the bean with the Faces context, for example if you want the bean, you can use:

FacesContext facesContext = FacesContext.getCurrentInstance();
Object o = facesContext.getApplication().evaluateExpressionGet(facesContext,
                    "#{phaseListenerBean}", Object.class);

And later use reflection to call the method or, You can call the method whith your expression, like this:

FacesContext fc = getContext();
ExpressionFactory factory = getExpressionFactory();
methodExpression = factory.createMethodExpression(
        fc.getELContext(), "#{phaseListenerBean.compListener}", 
                    Void.class, (Class<?>) null);
methodExpression.invoke(fc.getELContext(), null);

Please show some code for "compListener" and the utility class. Sorry for my bad english,

Cheers

Upvotes: 2

Related Questions