Value to update attribute of primefaces command button

13,835

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.

Share:
13,835
h-kach
Author by

h-kach

Learning is fun when the teachings and teachers are invaluable...

Updated on July 10, 2022

Comments

  • h-kach
    h-kach almost 2 years

    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.

  • h-kach
    h-kach almost 12 years
    If I give view source, the id of the component which has to be rendered will have the id prefixed by ":". So you mean it does not matter whether it has that id , I can still refer it by just the ID to render the component is it? I hope you got my point.
  • Matt Handy
    Matt Handy almost 12 years
    If they are inside the same container (and this is the case in your example) the simple client id of the component is just fine.
  • h-kach
    h-kach almost 12 years
    ok.. That clears all (Hopefully :)) . I will test in few other cases. Thanks for the clarifications.