Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSP/JSTL集合标记和循环_Jsp_Jstl - Fatal编程技术网

JSP/JSTL集合标记和循环

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

我的JSP页面有一个小问题。我试图循环遍历一组项目,并进行比较,以确保我查看的当前值与前一个值不同。代码如下所示:

<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]