【问题标题】:UIComponent getAttributes only gets last value from JSFUIComponent getAttributes 仅从 JSF 获取最后一个值
【发布时间】:2016-06-06 21:31:39
【问题描述】:

我有一个 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() 的调用将获取当前复合组件的值,但似乎它只是获取页面上最后一个复合组件的值。有人可以向我解释这些属性应该如何工作或我缺少什么吗?

【问题讨论】:

  • 您确定组件的行为方式是这样的吗?你确定你实际上没有调用错误的 a4j:jsFunction 吗?
  • 再一次,当您调用test() 时,您实际上希望调用哪一个,因为它们有多个?在 JavaScript 中,通常分配给变量的任何值/函数都会覆盖任何先前分配的值/函数。这个密切相关的问题可能包含您正在寻找的答案:stackoverflow.com/q/29124665
  • 非常感谢,BalusC。这就是问题所在。我在 jsFunction 名称后附加了一个 id 参数,现在它可以工作了。

标签: jsf composite-component ajax4jsf


【解决方案1】:

这里是解决方案,将 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2015-03-10
    • 2016-01-31
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多