index当前这次迭代从 0 开始的迭代索引
count当前这次迭代从 1 开始的迭代计数
first用来表明当前这轮迭代是否为第一次迭代的标志
last用来表明当前这轮迭代是否为最后一次迭代的标志
begin属性值
end属性值
step属性值
例:
表格偶数行与奇数行颜色交替效果
<c:forEach items="${queryPromotionList}" var="vPromotion" varStatus="vstatus">
<c:choose>
<c:when test="${vstatus.index%2==0}">
<tr bgcolor="#FFFFFF" height="40">
</c:when>
<c:otherwise>
<tr bgcolor="#F3F3F5" height="40">
</c:otherwise>
</c:choose>
<table class=table_body_bg cellspacing=1 cellpadding=1
width="100%" align=center border=0>
<c:forEach items="${list}" var="a" varStatus="vs">
<c:if test="${vs.count%5==1}">
<tr align="left" height="20">
</c:if>
<td class=table_body_td width="20%"><a href="/aam/degree/advisorAnswer.do?sfid=${a.sfid }">${a.xm }(${a.sfid })</a></td>
<c:set var="count" value="${vs.count}"/> //${vs.count}只在<c:forEach></c:forEach>的范围内有值 外部引用需要把值传出去
</c:forEach>
<c:if test="${count%5==1}">
<td class="table_body_td" width="20%"></td>
<td class="table_body_td" width="20%"></td>
<td class="table_body_td" width="20%"></td>
<td class="table_body_td" width="20%"></td>
</tr>
</c:if>
<c:if test="${count%5==2}">
<td class="table_body_td" width="20%"></td>
<td class="table_body_td" width="20%"></td>
<td class="table_body_td" width="20%"></td>
</tr>
</c:if>
<c:if test="${count%5==3}">
<td class="table_body_td" width="20%"></td>
<td class="table_body_td" width="20%"></td>
</tr>
</c:if>
<c:if test="${count%5==4}">
<td class="table_body_td" width="20%"></td>
</tr>
</c:if>
<c:if test="${count%5==0}">
</tr>
</c:if>
</table>
不论是对整数还是对集合进行迭代, <c:forEach>
剩余的属性 varStatus
所起的作用相同。和 var
属性一样, varStatus
用于创建限定了作用域的变量。不过,由 varStatus
属性命名的变量并不存储当前索引值或当前元素,而是赋予 javax.servlet.jsp.jstl.core.LoopTagStatus
类的实例。该类定义了一组特性,它们描述了迭代的当前状态,下面列出了这些特性:
特性 | Getter | 描述 |
current | getCurrent() | 当前这次迭代的(集合中的)项 |
index | getIndex() | 当前这次迭代从 0 开始的迭代索引 |
count | getCount() | 当前这次迭代从 1 开始的迭代计数 |
first | isFirst() | 用来表明当前这轮迭代是否为第一次迭代的标志 |
last | isLast() | 用来表明当前这轮迭代是否为最后一次迭代的标志 |
begin | getBegin() | begin 属性值 |
end | getEnd() | end 属性值 |
step | getStep() | step 属性值 |