Question

Is there a way to get the list of all client id of the contents of a particular div inside managed bean? I have the client id of the div. I have to call resetValue(...) on each component inside the div.

FacesContext.getCurrentInstance().getViewRoot().findComponent(...) will give me a single component if I provide the client id. I do not want to use it because I have a lot of components inside the div and this will have around 100 lines of code just to reset one div.

Is a way I can get the list of cient id so that I can iterate over this list and reset all the components? I am open to other approaches as well, as long as I can reset all components inside the div. My bean is managed by Spring (scope("view")). I use a a4j:commandButton to submit values to this bean. When I load the page again, I see the previously submitted values, which is incorrect. I tried resetValue(...) on one component inside the div after submission and it did reset the values of the component. Any help is appreciated.

Code sample:

xhtml:

<a4j:outputPanel id="EditSeaPage">
    <a4j:outputPanel id="editSeaContainer"
    rendered="#{seaBean.showEditSea}" layout="block"
    styleClass="switch-section">

        <h:panelGroup>
        <h:outputLabel value="#{messageSource.sea_label}" />
        <h:inputText id="updateSeaCode"
         value="#{seaBean.seaUIDTO.seaCode}"
         styleClass="user-detail">
         <a4j:ajax />
        </h:inputText>
    </h:panelGroup>
    <a4j:commandButton id="confirmUpdateSeaCode" execute="@this"
     action="#{seaBean.updateSeaCode}" render="parentContainer"/>
</a4j:outputPanel>
</a4j:outputPanel>

Parent xhtml:

<a4j:commandLink id="editSeaCode" value="edit"  onclick="hideMe()" render="editPortContainer"/>

Bean:

public String updateSeaCode() {
//does update
UIInput input = (UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent("updateSeaCode");
  input.resetValue();
   //This works. But I have around 20 input components in the above XHTML and can't write the //above code for each component
}

I just added a skeleton of what I am doing. How can I make sure the view gets updated when the call ends? To give me some more details, here is how it works: I have an edit link, which is an a4j:commandLink. On click of this link, I hide the link and display the editSeaContainer. If you see there is a rendered attribute. As soon as I click on the edit link I hide the link using jQuery and I make an a4j:jsFunction to set the boolean property to true so that this page is rendered. After the a4j:jsFunction I call a fetch method to retrieve the contents of this xhtml. By the time the method finishes, the boolean is set and the 'render' attribute renders the page. Everything is fine till here. But the input values do not update with the fetched values.

Thanks.

Was it helpful?

Solution

As far as JSF goes, the most straightforward way to get the list of ids of the children of a component is with the UIComponent#getChildren() and then iterating on the returned list to perform the reset:

    HtmlPanelGrid yourDiv = facesContext.getViewRoot().findComponent("theDivId");//facesContext is an instance of FacesContext
    List<UIComponent> children = yourDiv.getChildren();

        for(UIComponent child: children){

          if(child instanceof UIInput){ //resetValue is applicable only to input comps.
               ((UIInput)child).resetValue();
          }    
        } 

A more sweeping way to reset the values on an entire page is to call the same method on the UIViewRoot instance associated with that page as in:

    FacesContext ctxt = FacesContext.getCurrentInstance();
    UIViewRoot viewRoot = ctxt.getViewRoot();
    viewRoot.resetValues(ctxt,listOfIds);

Where listOfIds refers to a Collection<String> of clientIds belonging to components you want to perform a reset on. Obviously, this requires you to have the component ids beforehand.


JSF 2.2 has introduced the <f:resetValues/> tag (and resetValues attribute on the <f:ajax/> tag) to solve this specific problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top