<c:forEach>標簽有如下屬性:
| 屬性 | 描述 | 是否必要 | 默認值 |
|---|---|---|---|
| items | 要被循環的信息 | 否 | 無 |
| begin | 開始的元素(0=第一個元素,1=第二個元素) | 否 | 0 |
| end | 最后一個元素(0=第一個元素,1=第二個元素) | 否 | Last element |
| step | 每一次迭代的步長 | 否 | 1 |
| var | 代表當前條目的變量名稱 | 否 | 無 |
| varStatus | 代表循環狀態的變量名稱 | 否 | 無 |
<c:forEach>實例演示
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:forEach 標簽實例</title>
</head>
<body>
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>
運行結果如下:
Item 1 Item 2 Item 3 Item 4 Item 5
c:forEach varStatus屬性
current當前這次迭代的(集合中的)項
index當前這次迭代從 0 開始的迭代索引
count當前這次迭代從 1 開始的迭代計數
first用來表明當前這輪迭代是否為第一次迭代的標志
last用來表明當前這輪迭代是否為最后一次迭代的標志
begin屬性值
end屬性值
step屬性值
例如:
<c:forEach var="item" items="${contents}" varStatus="status">
<tr <c:if test="${status.count%2==0}">bgcolor="#CCCCFE"</c:if> align="left">
xxx
</tr>
</c:forEach>
