jsfcomposite-componentajax4jsf

UIComponent getAttributes only gets last value from JSF


I have a JSF 2.2 composite component which is used more than once on the same page.

@FacesComponent("myComponent")
public class MyComponent extends UIComponent {

    public void test() {
        System.out.printlin(getAttributes("value"));
    }
}

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

page xhtml

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

When I click on the first myComponent, it calls test() but prints the value of bean2.value instead of bean.value. If I remove line 2 then it prints bean.value. I thought that the call to getAttributes() would get the value for current composite component, but it seems that it is only getting the value for the last composite component on the page. Could someone explain to me how the attributes are supposed to work or what I am missing?


Solution

  • Here is the solution, with id attribute appended to the jsFunction to distinguish it from other same components on the page:

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