JSTL:从数组中删除最后一项 [英] JSTL: remove last item from array
问题描述
是否想知道是否可以使用JSTL从数组中删除最后一项?目前,我正在使用c:url将参数(来自数组)附加到超链接.我也希望能够删除最后一个参数...
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...
这是c:url附加参数的代码
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>
推荐答案
否,这是不可能的.您无法在JSTL中操作数组.您最多可以将最后一项设置为null
,但这不会更改数组的长度.
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>
请注意,我删除了fn:length()
检查,因为这是不必要的.如果没有项目,<c:forEach>
将不会进行迭代.
Note that I removed the fn:length()
check because that's unnecessary. The <c:forEach>
already won't iterate if there are no items.
这篇关于JSTL:从数组中删除最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!