【问题标题】:JSF 2.0 Get list of client id in managed beanJSF 2.0 获取托管 bean 中的客户端 ID 列表
【发布时间】:2014-04-28 14:41:31
【问题描述】:

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

FacesContext.getCurrentInstance().getViewRoot().findComponent(...) 如果我提供客户端 ID,将给我一个组件。我不想使用它,因为我在 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 以将布尔属性设置为true,以便呈现此页面。在a4j:jsFunction 之后,我调用了一个 fetch 方法来检索这个 xhtml 的内容。当方法完成时,布尔值被设置并且“render”属性呈现页面。到这里为止一切都很好。但是输入值不会随着获取的值而更新。

谢谢。

【问题讨论】:

  • 如果您使用 Ajax(看起来是这样)并且在响应中呈现列表,只需清除 bean 中的映射值字段。这意味着client.setValue("")
  • 如果您的意思是清除 bean 中的值,我已经这样做了,但这并没有帮助。这些值已正确填充到 bean 中,但视图仍显示旧值。
  • 那是因为 ajax 调用结束时视图没有更新。但是由于您没有提供代码的进一步示例,因此我无法推断出了什么问题。
  • 使用a4j:commandButtonrender属性:
  • 我刚刚编辑了这段代码,以便您更清楚地了解我在做什么。我使用了“render”属性,但这仅有助于渲染“div/a4j:outputPanel”。这仍然无助于更新输入组件值。

标签: jsf-2 ajax4jsf


【解决方案1】:

就 JSF 而言,获取组件子项 id 列表的最直接方法是使用 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 指的是属于您要对其执行重置的组件的客户端ID 的Collection&lt;String&gt;。显然,这需要您事先拥有组件 ID。


JSF 2.2 引入了&lt;f:resetValues/&gt; 标签(以及&lt;f:ajax/&gt; 标签上的resetValues 属性)来解决这个特定问题

【讨论】:

  • 谢谢!这就是我一直在寻找的。我使用的是 JSF 2.1.13,无法尝试 &lt;f:resetValues/&gt;,虽然这看起来更有吸引力。
  • 只是想指出第一个代码sn-p中的语法不正确。 FacesContext.getCurrentInstance().getViewRoot().findComponent("theDivId");
猜你喜欢
  • 2014-09-29
  • 2012-06-30
  • 1970-01-01
  • 2013-03-08
  • 2011-07-09
  • 2011-12-05
  • 1970-01-01
  • 2011-02-03
  • 2012-08-05
相关资源
最近更新 更多