【问题标题】:Address JSF component in ui:repeat在 ui:repeat 中寻址 JSF 组件
【发布时间】:2012-03-30 19:48:12
【问题描述】:

我正在尝试更新 UI 重复中的元素,但不幸的是,我仍然没有发现如何从 dataTable 中正确处理 outputPanel。我知道这个问题来自不同的命名容器,但我希望有一个解决方案。

<h:body>
    <h:form id="form">
        <ui:repeat var="_entry" value="#{test.entries}">
            <p:outputPanel id="counterPanel">
                <h:outputText value="#{test.counter}" />
            </p:outputPanel>

            <p:dataTable var="_p" id="paramTable" value="#{_entry.params}">
                <p:column headerText="Options">
                    <p:commandLink value="Update" update="counterPanel"  />
                </p:column>
            </p:dataTable>
        </ui:repeat>
    </h:form>
</h:body>

上面的代码示例引发了以下异常:

javax.servlet.ServletException: Cannot find component with identifier "counterPanel" in view.

谢谢, 雅各布

【问题讨论】:

  • 你能在生成的 html 源代码中看到 outputpanel 的 id 吗?也许在这里发布?

标签: ajax jsf primefaces


【解决方案1】:

两种方式:

  1. 您需要给&lt;ui:repeat&gt; 一个客户端ID,以便您可以通过绝对客户端ID 路径引用它:

    <h:form id="form">
        <ui:repeat id="entries" var="_entry" value="#{test.entries}">
            <p:outputPanel id="counterPanel">
                <h:outputText value="#{test.counter}" />
            </p:outputPanel>
    
            <p:dataTable var="_p" id="paramTable" value="#{_entry.params}">
                <p:column headerText="Options">
                    <p:commandLink value="Update" update=":form:entries:counterPanel" />
                </p:column>
            </p:dataTable>
        </ui:repeat>
    </h:form>
    
  2. &lt;h:form&gt; 移动到外循环内部,以便您可以使用@form

    <ui:repeat var="_entry" value="#{test.entries}">
        <h:form id="form">
            <p:outputPanel id="counterPanel">
                <h:outputText value="#{test.counter}" />
            </p:outputPanel>
    
            <p:dataTable var="_p" id="paramTable" value="#{_entry.params}">
                <p:column headerText="Options">
                    <p:commandLink value="Update" update="@form" />
                </p:column>
            </p:dataTable>
        </h:form>
    </ui:repeat>
    

【讨论】:

  • 最近我遇到了一种通过自下而上的方法获取命名容器 id 的方法:#{component.parent.parent.clientID}。然而,这很棘手,因为并非所有组件都会被渲染(例如复合组件命名容器)。你有什么看法?
  • @Matt:这在复合材料中特别有用,但不一定在普通模板中。我只会使用#{component.namingContainer.clientId} 来获取最近的父命名容器组件的客户端ID,或者使用#{component.namingContainer.parent.namingContainer.clientId} 来获取第二个父命名容器组件的客户端ID,等等。请注意,当命名容器本身是重复组件时,这将不起作用。它将返回带有行索引后缀的客户端 ID,这将导致“找不到组件”,因为行索引仅存在于客户端。
  • 这正是我们所经历的。感谢您的建议。
猜你喜欢
  • 2013-03-19
  • 2011-04-30
  • 2011-04-17
  • 2012-01-29
  • 1970-01-01
  • 2023-04-07
  • 2012-09-29
  • 1970-01-01
  • 2017-06-01
相关资源
最近更新 更多