Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

jsf - Iterate over nested List property inside h:datatable

    <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>

i got a entities member one of his property is a List photo with get/set that property is populate i don't know how to fetch that value in jsf i want only the first picture for each member since their have 2-3 photos. Its that possible?? any other solution will be appreciate.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just iterate over it using <ui:repeat> or <h:dataTable> the usual way. It's perfectly valid to nest multiple iterating components in each other. In case of <h:dataTable>, you only need to make sure that you put the nested iterating component inside a <h:column>.

E.g.

<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>

or

<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>

You'll potentially only run into issues when you nest multiple <ui:repeat> components and use <f:ajax> in it while using an older version of Mojarra.

Only JSTL <c:forEach> wouldn't work when nested inside a JSF iterating component for the reasons explained here JSTL in JSF2 Facelets... makes sense?


Unrelated to the concrete problem, please don't abuse <h:outputLabel> for pure text presentation. It generates a HTML <label> element which is intented to label an input element by for attribute. However, you're doing that nowhere in the code. You should be using <h:outputText> instead. By the way, I'm lately seeing this more often in code of starters. There must be somewhere a bad tutorial or resource which is abusing the <h:outputLabel> this way instead of <h:outputText> or even plain EL in template text. Which tutorial/resource was you using? Then I can contact the author about this severe misinstruction. See also Purpose of the h:outputLabel and its "for" attribute


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...