JSTL x:forEach以逗号分隔的输出

时间:2011-03-12 13:33:32

标签: xml jsp foreach jstl

我有一个包含以下内容的xml文件:

<authors>
    <author>name 1</author>
    <author>name 2</author>
    <author>name 3</author>
</authors>

我想用JSTL解析它,如:

name1, name2, name3

并且,如果超过3:

name1, name2, name3 et. al

使用<x:forEach ..>表示名称并以特定作者结尾,但是如何获取逗号并检查列表长度,我没有遇到任何问题?

1 个答案:

答案 0 :(得分:8)

varStatus属性与end属性结合使用。 varStatus引用了一个本地LoopTagStatus实例,该实例提供了多种getter方法,例如getIndex()isLast()end属性指定迭代应该结束的索引。

<x:forEach select="..." var="author" varStatus="loop" end="3">
    <c:if test="${loop.index lt 3}">${author}</c:if>
    <c:if test="${loop.index lt 2 and not loop.last}">,</c:if>
    <c:if test="${loop.index eq 3}">et. al</c:if>
</x:forEach>
相关问题