Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 943
Dims Avatar asked Jan 16 '12 15:01

Dims


3 Answers

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>
like image 130
JB Nizet Avatar answered Oct 17 '22 15:10

JB Nizet


<c:forEach items="${myList}" var="item" varStatus="status">
    ${item}<c:if test="${not status.last}">,</c:if>
</c:forEach>
like image 6
Viruzzo Avatar answered Oct 17 '22 14:10

Viruzzo


You may use `varStatus' attribute in EL,

<c:forEach var="foo" items="${foos}" varStatus="loopStatus">${foo}${!loopStatus.last?',':''}</c:forEach>` 
like image 3
Selvakumar Ponnusamy Avatar answered Oct 17 '22 14:10

Selvakumar Ponnusamy