JSTL:从数组中删除最后一项 [英] JSTL: remove last item from array

查看:73
本文介绍了JSTL:从数组中删除最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否想知道是否可以使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆