Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 将actionListener方法传递给组件内的commandButton_Jsf_Jsf 2_Composite Component - Fatal编程技术网

Jsf 将actionListener方法传递给组件内的commandButton

Jsf 将actionListener方法传递给组件内的commandButton,jsf,jsf-2,composite-component,Jsf,Jsf 2,Composite Component,首先,请原谅我的无知和无能,所以请使用搜索引擎(我发誓我已经搜索了很长时间,经常搜索,但没有找到任何令人满意的答案,这个问题) 我有一个bean实现了与action listener兼容的方法: @ManagedBean(name = "myBean") @ViewScoped class Bean{ public String myAction(ActionEvent event){ ... = event.getComponent().getAttributes().

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

我有一个bean实现了与action listener兼容的方法:

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

它被称为这样的东西:

<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(“某物”)
获取对象

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

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

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

尝试以下内容:

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

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

尝试以下操作:

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


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

谢谢Nikita!这似乎是一个很好的方法!无论如何,我在实际将方法传递给组件时遇到了一些问题,这意味着当pass
myAction=“#{myBean.myAction}”
时,它总是在搜索一个属性而不是一个方法,为了调试,我尝试了
myAction=“#{myBean.myAction(event)}”
,调用被传递给了该方法,是的!但是,在
myAction(ActionEvent)中{
事件为空…知道我是否可以从表达式上下文访问/传递事件吗?无论如何,谢谢!检查。你可以尝试添加
,然后这样使用:
谢谢Nikita!这似乎是一个很好的方法!无论如何,我在实际将方法传递到组件时遇到一些问题,意思是当is pass
myAction=“#{myBean.myAction}”时,它总是在搜索一个属性而不是一个方法,为了调试,我尝试了
myAction=“#{myBean.myAction(event)}”
,调用被传递到了方法,是的!但是,在
myAction(ActionEvent)中{
事件
为空…知道我是否可以从表达式上下文访问/传递事件吗?谢谢!检查。您可以尝试添加
,然后像这样使用: