我得到了一个包含以下内容的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 ..>输出姓名并以特定作者结尾没有问题,但是如何获得逗号并检查列表长度呢?
发布于 2011-03-12 21:54:16
将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>https://stackoverflow.com/questions/5282792
复制相似问题