<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
</head>
<body>
<h1>Just for test</h1>
<%-- 基本的应用 --%>
<c:forTokens items="A,B,C,D,E,F,G" delims="," var="item">
${item}
</c:forTokens>
<hr />
<%-- 设置begin和end的值 --%>
<c:forTokens items="A,B,C,D,E,F,G" delims="," var="item" begin="1" end="3">
${item}
</c:forTokens>
<hr />
<%-- 设置步长 --%>
<c:forTokens items="A,B,C,D,E,F,G" delims="," var="item" step="2">
${item}
</c:forTokens>
<hr />
<%-- 使用两种分割符,delims="l," --%>
<c:forTokens items="1,2,3,4,5,6|8|9|10" delims="|," var="item">
${item}
</c:forTokens>
<hr />
<%-- 动态数据加入 --%>
<%
request.setAttribute("items", "hanchao,tom,jerry|alax,jack,韩超,xiaoming");
%>
<c:forTokens var="item" items="${items}" delims="|," varStatus="status" begin="1" end="2">
${item }
<%-- <c:out value="${status.count}" /> //当前这次迭代从1开始的迭代计数 --%>
<c:out value="${status.index}" /> <!-- //当前这次迭代从0开始的迭代索引 -->
<c:if test="${status.last}"><%-- //用来表明当前这轮迭代是否为最后一次迭代 --%>
<br>总共被分为<c:out value="${status.count}" />段.
</c:if>
<c:if test="${status.first}"><%-- //用来表明当前这轮迭代是否为第一次迭代 --%>
<c:out value="第一次迭代!"/>
</c:if>
<br />
</c:forTokens>
<hr />
<%-- forEach的简单例子 --%>
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>
你可以参考的文章:
http://www.tutorialspoint.com/jsp/jstl_core_foreach_tag.htm
http://oodamuoo.blog.163.com/blog/static/169505349201091212146867/
http://shawnfree.iteye.com/blog/459918
转载于:https://blog.51cto.com/hanchaohan/1576136