关键在于<c:forEach>的varStatus属性,具体代码如下:
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<th>序号</th>
<th>姓名</th>
</tr>
<c:forEach var="list" items="${list}" varStatus="status">
<tr>
<td>${ status.index + 1}</td>
</table>
备注:status.index是从0开始的。
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<th>序号</th>
<th>姓名</th>
</tr>
<c:forEach var="list" items="${list}" varStatus="status">
<tr>
<td>${ status.index + 1}</td>
<td>${ student.name}</td>
</tr>
<tr>
<c:if test="${status.index%2==0}">
这是奇数
</c:if>
<c:if test="${status.index%2!=0}">
这是偶数
</c:if>
</tr>
</c:forEach></table>
备注:status.index是从0开始的。

本文介绍如何利用JSTL中的<c:forEach>标签遍历列表,并通过varStatus属性来获取当前项的状态信息,例如索引和奇偶判断,以便进行更灵活的数据展示。

被折叠的 条评论
为什么被折叠?



