Was wondering if it's possible to remove the last item from an array using JSTL? Currently I'm using c:url to append parameters (from an array) to a hyperlink. I want to be able to remove the last parameter as well...
Here is the code for the c:url to append parameters
<c:url value="search" var="url">
<c:param name="q" value="${q}"/>
<c:forEach var="field" items="${fq}">
<c:param name="fq" value="${field}"/>
</c:forEach>
</c:url>
No, that's not possible. You can't manipulate arrays in JSTL. You can at highest set the last item to null
, but that won't change the array's length.
In your particular case there's however another way: you can check if you're currently iterating over the last array item by checking LoopTagStatus#isLast()
and then just skip the item altogether in <c:param>
<c:url value="search" var="url">
<c:param name="q" value="${q}"/>
<c:forEach var="field" items="${fq}" varStatus="loop">
<c:if test="${not loop.last}">
<c:param name="fq" value="${field}"/>
</c:if>
</c:forEach>
</c:url>
Note that I removed the fn:length()
check because that's unnecessary. The <c:forEach>
already won't iterate if there are no items.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.