这些标签存在,作为一个很好的替代嵌入一个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