2014-02-28 63 views
0

我在我的JSP页面有一个小问题。我试图通过一系列项目进行循环,并进行比较以确保我所看到的当前值与之前的值不同。代码如下所示:JSP/JSTL集标签和循环

<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后。有什么方法可以检查循环中的项目的当前值与以前的值吗?

回答

0

您可以通过使用varStatus属性和EL括号注释:

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

所以,检查你是不是做了拳头迭代(由于没有以前的对象),使用varStatusfirst属性:

not i.first 

,然后比较基础上,varStatusindex财产

customerlist[i.index] eq customerlist[i.index - 1] 

或者,如果您确定列表中有更多项目,则可以使用begin="1"c:forEach跳过列表中的第一项。

+0

这对我有效。谢谢!我很感激。 –