JSTL for each separator

Is there a built-in function in JSTL to output delimiters when executing foreach?

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

+6
source share
3 answers

Use the varStatus attribute, which refers to 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> 
+16
source
 <c:forEach items="${myList}" var="item" varStatus="status"> ${item}<c:if test="${not status.last}">,</c:if> </c:forEach> 
+5
source

You can use varStatus attribute in EL,

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

Source: https://habr.com/ru/post/906126/


All Articles