JSTL的forTokens 和forEach 标签中的 varStatus 属性用于创建当前迭代的变量,并且赋予其 javax.servlet.jsp.jstl.core.LoopTagStatus 类的实例。该类定义了一组特性,它们描述了当前迭代对象的一些属性,下面列出了这些特性及其 描述(属性有(current 、index、count 、first 、last、begin 、end、step )由于排版老出现问题,贴出图片):
示例代码:
foreach标签用法:</br>
<%
List<String> list = new ArrayList<String>();
list.add("锄禾日当午");
list.add("汗滴禾下土");
list.add("谁知盘中餐");
list.add("粒粒皆辛苦");
request.setAttribute("list", list);
%>
遍历集合:</br>
<c:forEach items="${requestScope.list}" begin="0" end="4" step="1"
var="value" varStatus="status">
${status.index } ${status.current }</br>
</c:forEach>
取集合第一项和最后一项:</br>
<c:forEach items="${requestScope.list}" begin="0" end="4" step="1"
var="value" varStatus="status">
<c:if test="${status.first }">
${status.index } ${status.current }</br>
</c:if>
<c:if test="${status.last }">
${status.index } ${status.current }</br>
</c:if>
</c:forEach>