index當前這次迭代從 0 開始的迭代索引
count當前這次迭代從 1 開始的迭代計數
first用來表明當前這輪迭代是否為第一次迭代的標志
last用來表明當前這輪迭代是否為最后一次迭代的標志
begin屬性值
end屬性值
step屬性值
例:
表格偶數行與奇數行顏色交替效果
引標簽庫
<%@ taglib prefix="c" uri="<%@ taglib prefix="fn" uri="<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 屬性值 |