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

来源:互联网 发布:安全知识网络答题入口 编辑:程序博客网 时间:2024/06/17 00:19

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

属性:

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

属性描述必须?默认itemsInformation to loop overNoNonebeginElement to start with (0 = first item, 1 = second item, ...)No0endElement to end with (0 = first item, 1 = second item, ...)NoLast elementstepProcess every step itemsNo1varName of the variable to expose the current itemNoNonevarStatusName of the variable to expose the loop statusNoNone

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

属性描述必须?默认delimsCharacters to use as delimitersYesNone

<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 1Item 2Item 3Item 4Item 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>

这将产生以下输出结果:

Zaranuharoshy
0 1
原创粉丝点击