可以在循环中添加一个变量来记录循环次数,作为行号显示:
<c:if test="${not empty employees}">
<c:forEach var="employee" items="${employees}" varStatus="status">
<tr>
<td>${status.count}</td>
<td>${employee.empno}</td>
<!-- other columns -->
</tr>
</forEach>
</
varStatus="status"定义了一个状态对象status,它有一个count属性可以记录当前是第几次循环。
每行就可以直接显示${status.count}来输出行号。
另外,也可以使用下标来记录:
<c:if test="${not empty employees}">
<c:forEach var="employee" items="${employees}" varStatus="status" begin="1">
<tr>
<td>${status.index}</td>
<!-- other columns -->
</tr>
</forEach>
</
begin="1"从1开始计数。status.index就是从1开始的下标。
两种方法都可以很方便地在表格每行添加行号列。