JSTL forEach separator

后端 未结 3 1059
你的背包
你的背包 2021-01-12 13:48

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

相关标签:
3条回答
  • 2021-01-12 13:58
    <c:forEach items="${myList}" var="item" varStatus="status">
        ${item}<c:if test="${not status.last}">,</c:if>
    </c:forEach>
    
    0 讨论(0)
  • 2021-01-12 14:03

    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>
    
    0 讨论(0)
  • 2021-01-12 14:09

    You may use `varStatus' attribute in EL,

    <c:forEach var="foo" items="${foos}" varStatus="loopStatus">${foo}${!loopStatus.last?',':''}</c:forEach>` 
    
    0 讨论(0)
提交回复
热议问题