user882866
user882866

Reputation:

JSF2: Passing a method in an EL expression

is it possible to pass a method in an EL expression?

I have one bean and two views. The second view has a button but which method the button triggers should be defined by the first view. So I have to tell the second view the method whe n I link from the first view.

I imagine something like this:

First View:

<h:link outcome="secondView.xhtml" value="Second view with method A">
    <f:param name="methodToCall" value="#{bean.methodA}">
</h:link>
<h:link outcome="secondView.xhtml" value="Second view with method B">
    <f:param name="methodToCall" value="#{bean.methodB}">
</h:link>

Second view:

<h:commandButton action="#{methodToCall}" value="Call the method" />

Upvotes: 0

Views: 1621

Answers (3)

BalusC
BalusC

Reputation: 1108547

No, that's not possible. You can however invoke dynamic bean methods using the brace notation [].

<h:link outcome="secondView.xhtml" value="Second view with method A">
    <f:param name="methodToCall" value="methodA">
</h:link>
<h:link outcome="secondView.xhtml" value="Second view with method B">
    <f:param name="methodToCall" value="methodB">
</h:link>

with

<h:commandButton action="#{bean[param.methodToCall]}" value="Call the method" />

If the bean needs to be dynamic as well, you'll have to pass the bean name along separately and know its scope.

<h:link outcome="secondView.xhtml" value="Second view with method A">
    <f:param name="beanToCall" value="bean">
    <f:param name="methodToCall" value="methodA">
</h:link>
<h:link outcome="secondView.xhtml" value="Second view with method B">
    <f:param name="beanToCall" value="bean">
    <f:param name="methodToCall" value="methodB">
</h:link>

with

<h:commandButton action="#{requestScope[param.beanToCall][param.methodToCall]}" value="Call the method" />

Upvotes: 3

James DW
James DW

Reputation: 1815

I don't think there is a way to do this in JSF. My suggestion would be to have the call from the first view select a delegate in the backing bean which is called when clicking the action from the second view.

Something like this

public class Bean {

    public interface Delegate {
        void doSomething();
    }

    public class MethodADelegate implements Delegate {
        public void doSomething() {

        }
    }

    public class MethodBDelegate implements Delegate {
        public void doSomething() {

        }
    }

    private Delegate delegate;

    public String methodA() {
        this.delegate = new MethodADelegate();
        return "view2";
    }

    public String methodB() {
        this.delegate = new MethodBDelegate();
        return "view2";
    }

    public String view2Call() {
        delegate.doSomething();
        return "done";
    }
}

<h:commandLink action="#{bean.methodA}" value="Second view with method A" />
<h:commandLink action="#{bean.methodB}" value="Second view with method B" />

<h:commandButton action="#{bean.view2Call}" value="Call the method" />

Upvotes: 0

andbi
andbi

Reputation: 4454

Yes, it's possible. Here is the description for action attribute:

MethodExpression representing the application action to invoke when this component is activated by the user. The expression must evaluate to a public method that takes no parameters, and returns an Object (the toString() of which is called to derive the logical outcome) which is passed to the NavigationHandler for this application.

Upvotes: 0

Related Questions