Dims
Dims

Reputation: 51109

JSTL forEach separator

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?

Upvotes: 7

Views: 6353

Answers (3)

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5543

You may use `varStatus' attribute in EL,

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

Upvotes: 3

Viruzzo
Viruzzo

Reputation: 3025

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

Upvotes: 6

JB Nizet
JB Nizet

Reputation: 691865

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>

Upvotes: 17

Related Questions