JSTL <c:forEach>,<c:forTokens>标签

这些标签存在,作为一个很好的替代嵌入一个Java的for, while, 或 do-while 通过一个scriptlet循环。 <c:forEach>标记是较为常用的标签,因为它遍历对象的集合。<c:forTokens>标签是用来打破一个字符串转换成标记和遍历每个令牌。

属性:

<c:forEach>标记具有以下属性:

属性 描述 必须? 默认
items Information to loop over No None
begin Element to start with (0 = first item, 1 = second item, ...) No 0
end Element to end with (0 = first item, 1 = second item, ...) No Last element
step Process every step items No 1
var Name of the variable to expose the current item No None
varStatus Name of the variable to expose the loop status No None

<c:forTokens>标签有类似<c:forEach>的属性,除了一个额外的属性delims,它指定的字符作为分隔符使用。

属性 描述 必须? 默认
delims Characters to use as delimiters Yes None

<c:forEach>实例:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:forEach> Tag Example</title>
</head>
<body>
<c:forEach var="i" begin="1" end="5">
   Item <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>

这将产生以下结果:

Item 1
Item 2
Item 3
Item 4
Item 5

<c:forTokens>实例:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:forTokens> Tag Example</title>
</head>
<body>
<c:forTokens items="Zara,nuha,roshy" delims="," var="name">
   <c:out value="${name}"/><p>
</c:forTokens>
</body>
</html>

这将产生以下输出结果:

Zara
nuha
roshy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值