【问题标题】:Pass actionListener method to commandButton within a component将 actionListener 方法传递给组件内的 commandButton
【发布时间】:2012-04-27 09:27:46
【问题描述】:

首先,请原谅我的无知和无能,所以请使用搜索引擎(我发誓我已经搜索了很长时间并且经常搜索,但没有找到任何令人满意的答案)。

我有一个实现动作监听器兼容方法的 bean:

@ManagedBean(name = "myBean")
@ViewScoped
class Bean{
    public String myAction(ActionEvent event){
        ... = event.getComponent().getAttributes().get("something");
    }
}

然后,我有一个这样的 jsf 组件:

<composite:interface>
    <composite:attribute name="actionBean" required="true"/>
    <composite:attribute name="actionMethod" method-signature="void myAction(javax.faces.event.ActionEvent)" />
</composite:interface>
<composite:implementation>
        <h:form>
            <p:commandButton actionListener="#{cc.attrs.actionBean[cc.attrs.actionMethod]}">
                <f:attribute name="something" value="somevalue" />
            </p:commandButton>
        </h:form>
</composite:implementation>

它是这样称呼的:

<namespace:myComponent actionBean="#{myBean}" actionMethod="myAction" />

我知道这个调用不起作用,我想知道如何正确地做!

我的主要意图是我想要一个相对通用的 jsf 组件(以后可以重用它会很好),它包含一个按钮。单击此按钮时,我想传递一个对象(不是简单的字符串!如果是字符串,我将使用action="..." 并通过f:param 传递它)。使用actionListener 方法,我通过event.getComponent().getAttributes().get("something") 获取对象。

我认为签名void myAction(javax.faces.event.ActionEvent) 是破坏将相关方法传递给组件的问题,不是吗?通常是否可以将带有任何参数的方法传递给 jsf 组件(如果可以,如何)?

所以,我希望有一个可能的解决方案来解决改变上述策略的一般问题,或者使用一些好的和不同的东西(一般来说,我不喜欢使用任何黑客或变通方法,但喜欢使用框架)。

如果有人能找到时间给我指路,谢谢!如果这个问题已经存在,最好访问相关帖子并将其删除。

【问题讨论】:

    标签: jsf jsf-2 composite-component


    【解决方案1】:

    尝试以下操作:

    <namespace:myComponent myAction="#{myBean.myAction}"/>
    

    和复合组件:

    <composite:interface>
        <composite:attribute name="myAction" 
                             required="true" 
                             method-signature="void myAction(javax.faces.event.ActionEvent)"
                             targetAttributeName="actionListener"/>
    </composite:interface>
    <composite:implementation>
        <h:form>
            <p:commandButton id="myAction">
                <f:attribute name="something" value="somevalue" />
            </p:commandButton>
        </h:form>
    </composite:implementation>
    

    查看composite:attribute 文档。它有几个选项可以将侦听器传递给复合组件。我为此使用了targetAttributeName

    【讨论】:

    • 谢谢尼基塔!这似乎是一个不错的方法!无论如何,我在将方法实际传递给组件时遇到了一些问题,这意味着何时传递 myAction="#{myBean.myAction}" 它总是在搜索 attribute 而不是方法,为了调试我尝试了 myAction="#{myBean.myAction(event)}",并且调用已传递给方法,是的!但是,在myAction(ActionEvent event){ 中,event 为空……知道我是否可以从表达式上下文中访问/传递event?还是谢谢!
    • 检查docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/…。您可以尝试添加&lt;composite:actionSource name="myAction" /&gt;,然后像这样使用:&lt;f:actionListener for="myAction" binding="#{bean.allEventsListener}" /&gt;
    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 2017-10-19
    相关资源
    最近更新 更多