h-kach
h-kach

Reputation: 351

Value to update attribute of primefaces command button

I have a query here regarding the value given to the update attribute of the primefaces command button.

Here is a simple code. I have a simple xhtml page

<h:body>
<h:form id="main">
    <p:growl id="maingrowl" showDetail="true"></p:growl>
    <p:panelGrid id="mainpanel" columns="2">

            <p:commandButton value="Ajax Submit" update="test" id="ajax"
                actionListener="#{mytestBean.display}" />


    </p:panelGrid>

    <p:panelGrid id="test" rendered="#{mytestBean.show}" columns="1">
        <h:inputText value="#{mytestBean.val1}" />
        <h:inputText value="#{mytestBean.val2}" />
        <p:commandButton value="value in" id="aj" update="test"
            actionListener="#{mytestBean.displaysec}">
            </p:commandButton>
    </p:panelGrid>

</h:form>
</h:body>

Now, If I have to update the panelgrid "test" then in the first command button I have to provide the id "test" prefixed with ":main:" in the update attribute of the command button since that is the exact id in the HTML DOM as I find it in the view source in the browser. But, I was able to update even by given just "test" in the update attribute. So my question here is when should I use the ":" in the id or is it mandatory or is there any pre defined rule to give the id's in the attribute?

Also, I am aware of the fact that the id given in the update attribute is through the UINamingContainer in which it is placed. Please pardon me if its trivial, I am really confused here.

Any help greatly appreciated. Thank you.

Upvotes: 2

Views: 14269

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

If both components (the one to update and the one that triggers the update) are inside the same NamingContainer, the relative client id withoud the : is sufficient.

NamingContainers in jsf are all those components that prepend their own id to the client id of all child components, e.g. <h:form>, <h:dataTable> or <ui:repeat> (or to be more precise: all components that implement the javax.faces.component.NamingContainer interface).

If both components are not inside the same NamingContainer, you have to use the absolute id beginning with a : from the view root.

If you are interested in more detailed information I recommend reading the javadoc of the UIComponent#findComponent() method where the search algorithm is described.

Note that the : is the default character to separate segments of a clientId. You can change it with a context parameter in your web.xml.

Upvotes: 4

Related Questions