Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jstl foreach omit an element in last record

Tags:

jsp

jstl

trying to use this jstl to formulate a json string, how can i make the segment not to put a comma in the end of the last record? note the comma in the end

<c:forEach items="${fileList}" var="current">     { id:1001,data:["<c:out value="${current.fileName}" />" , "<c:out value="${current.path}" />" , "<c:out value="${current.size}" />" , "<c:out value="${current.type}" />"] }, </c:forEach> 
like image 1000
nokheat Avatar asked Jun 23 '10 05:06

nokheat


1 Answers

Just use LoopTagStatus#isLast().

<c:forEach items="${fileList}" var="current" varStatus="loop">     { id: 1001,       data: [         "<c:out value="${current.fileName}" />",         "<c:out value="${current.path}" />",         "<c:out value="${current.size}" />",         "<c:out value="${current.type}" />"       ]     }<c:if test="${!loop.last}">,</c:if> </c:forEach> 

You can also use the conditional operator in EL instead of <c:if>:

    ${!loop.last ? ',' : ''} 
like image 93
BalusC Avatar answered Sep 23 '22 17:09

BalusC