UIComponent getAttributes 仅从 JSF 获取最后一个值



我有一个JSF 2.2复合组件,它在同一页面上被多次使用。

@FacesComponent("myComponent")
public class MyComponent extends UIComponent {
    public void test() {
        System.out.printlin(getAttributes("value"));
    }
}

组件 XHTML:

<composite:interface componentType="myComponent">
    <composite:attribute name="value" required="true" type="java.lang.String" />
</composite:interface>
<composite:implementation>
    <a4j:jsFunction name="test" action="#{cc.test()}" execute="input" />
    <s:span id="input">
        <h:inputText value="#{cc.attrs.value}" onclick="test()" />
    </s:span>
</composite:implementation>

页面 xhtml

<my:myComponent value="#{bean.value}" />  -- line 1
<my:myComponent value="#{bean2.value}" /> -- line 2

当我单击第一个 myComponent 时,它会调用 test(),但打印的是 bean2.value 的值而不是 bean.value。如果我删除第 2 行,那么它会打印 bean.value。我以为对getAttributes()的调用会获取当前复合组件的值,但似乎它只获取页面上最后一个复合组件的值。有人可以向我解释这些属性应该如何工作或我缺少什么吗?

这是解决方案,将 id 属性附加到 jsFunction 以将其与页面上的其他相同组件区分开来:

<composite:interface componentType="myComponent">
    <composite:attribute name="id" type="java.lang.String" />
    <composite:attribute name="value" required="true" type="java.lang.String" />
</composite:interface>
<composite:implementation>
    <a4j:jsFunction name="#{cc.attrs.id}test" action="#{cc.test()}" execute="input" />
    <s:span id="input">
        <h:inputText value="#{cc.attrs.value}" onclick="#{cc.attrs.id}test()" />
    </s:span>
</composite:implementation>

最新更新