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

Mic*_*ert 2 xml foreach jsp jstl

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

<authors>
    <author>name 1</author>
    <author>name 2</author>
    <author>name 3</author>
</authors>
Run Code Online (Sandbox Code Playgroud)

我想用JSTL解析它到以下列表:

name1, name2, name3
Run Code Online (Sandbox Code Playgroud)

并且,如果超过3:

name1, name2, name3 et. al
Run Code Online (Sandbox Code Playgroud)

我没有问题使用一个<x:forEach ..>发出名称并以特定作者结束,但如何获取逗号并检查列表长度?

Bal*_*usC 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>
Run Code Online (Sandbox Code Playgroud)