简体   繁体   English

JSF2:在EL表达式中传递方法

[英]JSF2: Passing a method in an EL expression

is it possible to pass a method in an EL expression? 是否可以在EL表达式中传递方法?

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" />

No, that's not possible. 不,那不可能。 You can however invoke dynamic bean methods using the brace notation [] . 但是,您可以使用大括号符号[]调用动态bean方法。

<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. 如果Bean也需要动态,则必须分别传递Bean名称并知道其范围。

<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" />

Yes, it's possible. 是的,有可能。 Here is the description for action attribute: 这是action属性的描述:

MethodExpression representing the application action to invoke when this component is activated by the user. MethodExpression表示当用户激活此组件时要调用的应用程序操作。 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. 该表达式必须求值为不带参数的公共方法,并返回一个对象(将调用该对象的toString()以得出逻辑结果),该对象将传递给此应用程序的NavigationHandler。

I don't think there is a way to do this in JSF. 我认为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. 我的建议是让第一个视图中的调用在第二个视图中单击动作时在后备Bean中选择一个委托。

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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM