JSTL <c:forEach >的使用

本文介绍JSP中使用<c:forEach>指令进行循环的各种方法,包括整数循环、数组循环、字符串数组循环等,并展示了如何结合<c:if>指令实现条件判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、整数
<c:forEach var="i" begin="1" end="10" step="1">
<c:out value="${i}" />,
</c:forEach><p>
二、计算x的平方
<table>
<tr><th>Value</th>
<th>Square</th></tr>
<c:forEach var="x" begin="0" end="10" step="2">
<tr><td><c:out value="${x}"/></td>
<td><c:out value="${x * x}"/></td></tr>
</c:forEach>
</table> <p>
三、字符串"47,52,53,55,46,22,16,2" 分隔.
<table border="1">
<c:forTokens items="47,52,53,55,46,22,16,2" delims="," var="dailyPrice">
<tr><td><c:out value="${dailyPrice}"/></td></tr>
</c:forTokens>
</table><p>
四、使用步长
<table>
<tr><th>second</th>
<th>second</th></tr>
<c:forEach var="seconds" begin="0" end="${ pageContext.session.maxInactiveInterval}" step="60">
<tr><td><c:out value="${seconds}"/></td>
<td><c:out value="${seconds}"/></td></tr>
</c:forEach>
</table> <p>
五、对数组进行循环

<% int ai[] = {10, 20, 30, 40, 50};
pageContext.setAttribute("ary", ai);
%>
<c:forEach var="i" items="${ary}">
<c:out value="${i}"/>*
</c:forEach><p>

<%
Cookie c=new Cookie("cookie1","one");
response.addCookie(c);
%>

<%
Cookie cookies[]=request.getCookies();
Cookie sCookie=null;
String sname=null;
String name=null;
if(cookies==null) // 如果没有任何cookie
out.print("none any cookie");
else
{
//out.print(cookies.length + "<br>");
for(int i=0;i<cookies.length; i++) // 循环列出所有可用的Cookie
{
sCookie=cookies[i];
sname=sCookie.getName();
name = sCookie.getValue();
out.println(sname + "->" + name + "<br>");
}
}
%>

<table border="1" align="center">
<tr><th>Cookie Name</th>
<th>Cookie Value</th></tr>
<c:forEach var="cook" items="${pageContext.request.cookies}">
<tr><td><c:out value="${ cook.name}"/></td>
<td><c:out value="${cook.value}"/></td></tr>
</c:forEach>
</table> <p>

六、字符串数组循环
<% String as[] = {
"A first string", "La deuxieme string", "Ella troisiemo stringo"
};
request.setAttribute("stringArray", as);
%><p>
<c:forEach var="string" items="${stringArray}">
<c:out value="${string}"/><br>
</c:forEach>
七、枚举
<%
Hashtable hashtable1 = new Hashtable();
pageContext.setAttribute("numberMap", hashtable1);

hashtable1.put(new Integer(1), "uno");
hashtable1.put(new Integer(2), "dos");
hashtable1.put(new Integer(3), "tres");
hashtable1.put(new Integer(4), "cuatro");
hashtable1.put(new Integer(5), "cinco");
hashtable1.put(new Integer(6), "seis");
hashtable1.put(new Integer(7), "siete");
hashtable1.put(new Integer(8), "ocho");
hashtable1.put(new Integer(9), "nueve");
hashtable1.put(new Integer(10), "diez");

java.util.Enumeration enumeration = hashtable1.keys();
pageContext.setAttribute ("enumeration", enumeration);
%>

<c:forEach var="item" items="${enumeration}" begin="2" end="10" step="2">
<c:out value="${item}"/><br>
</c:forEach><p>
八、map

<c:forEach var="prop" items="${numberMap}" begin="1" end="5">
<c:out value="${prop.key}"/> = <c:out value="${prop.value }"/><br> <c:out value="${prop[key]}"/>
</c:forEach>

九、和<fn:split>一起使用

<c:forTokens items="${remind.remindWay}" delims="," var="i">

</c:forTokens>

<c:set var="array" value="${fn:split(remind.remindWay, ',')}" />
<c:forEach var="i" items="${array}">
<c:if test="${i eq '1'}">系统短信</c:if>
<c:if test="${i eq '2'}">手机短信</c:if>
<c:if test="${i eq '3'}">电子邮件</c:if>
</c:forEach>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值