JSF 2.0获取托管bean中的客户端ID列表

时间:2014-03-21 09:46:53

标签: jsf-2 ajax4jsf

有没有办法获取托管bean中特定div内容的所有客户端ID列表?我有div的客户端ID。我必须在div中的每个组件上调用resetValue(...)

如果我提供客户端ID,

FacesContext.getCurrentInstance().getViewRoot().findComponent(...)会给我一个组件。我不想使用它,因为我在div中有很多组件,这将有大约100行代码只是为了重置一个div。

我是否可以获取cient id列表,以便我可以遍历此列表并重置所有组件?我也对其他方法持开放态度,只要我可以重置div中的所有组件。 我的bean由Spring(scope("view"))管理。我使用a4j:commandButton向此bean提交值。当我再次加载页面时,我看到以前提交的值,这是不正确的。我在提交后在div中的一个组件上尝试resetValue(...),它确实重置了组件的值。 任何帮助表示赞赏。

代码示例:

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>

父xhtml:

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

豆:

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
}

我刚刚添加了我正在做的事情的骨架。如何在通话结束时确保视图得到更新?为了向我提供更多详细信息,以下是它的工作原理:我有一个edit链接,即a4j:commandLink。点击此链接,我隐藏了链接并显示editSeaContainer。如果您看到有渲染属性。只要我点击edit链接,我就会使用jQuery隐藏链接,然后我创建一个a4j:jsFunction来将boolean属性设置为true,以便呈现此页面。在a4j:jsFunction之后我调用了一个fetch方法来检索这个xhtml的内容。当方法完成时,设置布尔值并且'render'属性呈现页面。一切都很好,直到这里。但输入值不会随获取的值一起更新。

感谢。

1 个答案:

答案 0 :(得分:0)

就JSF而言,获取组件子级列表的最简单方法是使用UIComponent#getChildren(),然后在返回的列表上进行迭代以执行重置:

    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();
          }    
        } 

在整个页面上重置值的更彻底的方法是在与该页面关联的UIViewRoot实例上调用相同的方法,如下所示:

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

listOfIds指的是属于您要执行重置的组件的Collection<String>个clientIds。显然,这需要您事先获得组件ID。


JSF 2.2在<f:resetValues/>标记上引入了resetValues标记(以及<f:ajax/>属性)来解决此特定问题