JSP/JSTL集合标记和循环
我的JSP页面有一个小问题。我试图循环遍历一组项目,并进行比较,以确保我查看的当前值与前一个值不同。代码如下所示:JSP/JSTL集合标记和循环,jsp,jstl,Jsp,Jstl,我的JSP页面有一个小问题。我试图循环遍历一组项目,并进行比较,以确保我查看的当前值与前一个值不同。代码如下所示: <c:set var="previousCustomer" value=""/> <c:forEach items="${customerlist}" var="customer" varStatus="i"> <c:choose> <c:when test="${(customer.account) != (previo
<c:set var="previousCustomer" value=""/>
<c:forEach items="${customerlist}" var="customer" varStatus="i">
<c:choose>
<c:when test="${(customer.account) != (previousCustomer)}">
[do some stuff]
</c:when>
<c:otherwise>
[do other stuff]
</c:otherwise>
</c:choose>
<c:set var="previousCustomer" value="${customer.account}"/>
</c:forEach>
[做些事情]
[做其他事情]
但是,当我写出值时,previousCustomer在设置为customerlist.account后总是返回与customerlist.account相同的值。有没有办法检查循环中项目的当前值与以前的值?您可以使用
varStatus
属性和大括号表示法:
<c:forEach items="${customerlist}" var="customer" varStatus="i">
<c:choose>
<c:when test="${not i.first and customerlist[i.index] eq customerlist[i.index - 1]}">
[do some stuff]
</c:when>
<c:otherwise>
[do other stuff]
</c:otherwise>
</c:choose>
</c:forEach>
然后根据varStatus
index
属性进行比较
customerlist[i.index] eq customerlist[i.index - 1]
或者,如果您确定列表中有更多项目,可以使用c:forEach
中的begin=“1”
跳过列表中的第一项
customerlist[i.index] eq customerlist[i.index - 1]