【问题标题】:Iterate over nested List property inside h:datatable迭代 h:datatable 内的嵌套 List 属性
【发布时间】:2013-05-11 13:19:45
【问题描述】:
    <h:dataTable value="#{SearchingBeans.list}" var="entry">
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Photo</h:outputLabel>
            </f:facet>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Pseudo</h:outputLabel>
            </f:facet>
            <h:outputLabel value="#{entry.pseudo}"></h:outputLabel>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Description</h:outputLabel>
            </f:facet>
            <h:outputLabel value="#{entry.description}"></h:outputLabel>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Photo</h:outputLabel>
            </f:facet>
            <h:outputLabel value="#{entry.photo[0].path}"></h:outputLabel> <-- this a List
        </h:column>
    </h:dataTable>

我有一个实体成员,他的属性之一是带有 get/set 的列表照片 该属性已填充 我不知道如何在 jsf 中获取该值我只想要每个成员的第一张图片,因为他们有 2-3 张照片。这可能吗??任何其他解决方案将不胜感激。

【问题讨论】:

  • 如果你有entry.list 类型为List,你可以用EL empty 检查它的内容,然后用[0] 访问它的第一个元素,如果我正确理解你的想法的话。

标签: jsf datatable nested


【解决方案1】:

只需按照通常的方式使用&lt;ui:repeat&gt;&lt;h:dataTable&gt; 对其进行迭代。将多个迭代组件相互嵌套是完全有效的。对于&lt;h:dataTable&gt;,您只需确保将嵌套的迭代组件放入&lt;h:column&gt;

例如

<h:dataTable value="#{bean.entities}" var="entity">
    <h:column>
        #{entity.property}
    </h:column>
    <h:column>
        <ui:repeat value="#{entity.subentities}" var="subentity">
            #{subentity.property}
        </ui:repeat>
    </h:column>
</h:dataTable>

<h:dataTable value="#{bean.entities}" var="entity">
    <h:column>
        #{entity.property}
    </h:column>
    <h:column>
        <h:dataTable value="#{entity.subentities}" var="subentity">
            <h:column>
                #{subentity.property}
            </h:column>
        </h:dataTable>
    </h:column>
</h:dataTable>

只有在嵌套多个 &lt;ui:repeat&gt; 组件并在其中使用 &lt;f:ajax&gt; 并使用旧版本的 Mojarra 时,您才可能会遇到问题。

只有 JSTL &lt;c:forEach&gt; 嵌套在 JSF 迭代组件中时无法工作,原因在此处 JSTL in JSF2 Facelets... makes sense? 中进行了解释


与具体问题无关,请不要滥用&lt;h:outputLabel&gt;纯文本演示。它生成一个 HTML &lt;label&gt; 元素,该元素旨在通过 for 属性指向 label an input element。但是,您在代码中没有这样做。您应该改用&lt;h:outputText&gt;。顺便说一句,我最近在启动代码中经常看到这种情况。一定有一个糟糕的教程或资源以这种方式滥用&lt;h:outputLabel&gt;,而不是模板文本中的&lt;h:outputText&gt;,甚至是纯EL。您使用的是哪个教程/资源?然后我可以就这个严重的误导联系作者。另见Purpose of the h:outputLabel and its "for" attribute

【讨论】:

  • 告诉你的老师花点时间学习基本的 HTML。他显然对 HTML 语义一无所知。
  • @BalusC '我可以就这个严重的误导联系作者',这真是个好主意。你真的很聪明。也许它是像 r​​oseindia 这样臭名昭著的 JSF 相关网站之一?
  • 多么简单,我们却把它弄得这么复杂
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 2022-08-19
  • 2021-06-05
  • 2013-02-04
相关资源
最近更新 更多