JSTL forEach separator

流过昼夜 提交于 2019-12-01 02:42:26

问题


Is there a built in feature in JSTL to output separators while doing foreach?

The task is to output separators (like commas) after each iteration except the last one (or before each except the first). Is there any ELSE tag for foreach?


回答1:


Use the varStatus attribute, which references an object of type LoopTagStatus:

<c:forEach var="foo" items="${foos}" varStatus="loopStatus">
    <c:out value="${foo}"/>
    <c:if test="${!loopStatus.last}"> | </c:if>
</c:forEach>



回答2:


<c:forEach items="${myList}" var="item" varStatus="status">
    ${item}<c:if test="${not status.last}">,</c:if>
</c:forEach>



回答3:


You may use `varStatus' attribute in EL,

<c:forEach var="foo" items="${foos}" varStatus="loopStatus">${foo}${!loopStatus.last?',':''}</c:forEach>` 


来源:https://stackoverflow.com/questions/8882528/jstl-foreach-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!