【问题标题】:JSF: logic based on iteration indexJSF:基于迭代索引的逻辑
【发布时间】:2010-10-25 13:28:41
【问题描述】:

原谅我的标题,这是我有限的大脑这么晚才想到的最好的。

所以,我有一个字符串列表,类似于 [abc, def, ghi]。

问题:在 JSF 中,我如何迭代列表并创建一个看起来像“abc, def, ghi”的字符串(注意逗号)?

对于那些急于告诉我最好使用 Java 方法来连接字符串的人,请听听:列表的每个成员都应呈现为单独的 commandLink。

如果是纯 JSF,它看起来像:

<h:commandLink>abc</h:commandLink>, <h:commandLink>def</h:commandLink>, <h:commandLink>ghi</h:commandLink>

【问题讨论】:

    标签: jsf richfaces facelets icefaces


    【解决方案1】:

    假设#{bean.items} 返回List&lt;String&gt;String[],在JSF 1.x 中您可以使用JSTL c:forEachvarStatus。它为您提供了 LoopTagStatus 的句柄,该句柄具有 isLast() 方法。

    <c:forEach items="#{bean.items}" var="item" varStatus="loop">
        <h:commandLink value="#{item}" /><c:if test="#{!loop.last}">, </c:if>
    </c:forEach>
    

    在 JSF 2.x 附带的 Facelets 中,ui:repeat 提供相同的功能。

    <ui:repeat value="#{bean.items}" var="item" varStatus="loop">
        <h:commandLink value="#{item}" />#{!loop.last ? ', ' : ''}
    </ui:repeat>
    

    【讨论】:

    • 您在 上的解决方案对我来说不是开箱即用的,但它引导我访问了这个论坛帖子 seamframework.org/Community/…,其中包含我的答案等等。谢谢..
    • 它只适用于 JSF 2.x。您似乎正在使用 JSF 1.x。第一个解决方案更适用于您。
    猜你喜欢
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 2017-03-24
    相关资源
    最近更新 更多