您的位置:首页 > Web前端 > JavaScript

JSTL标签库_3_cforEach_cforTokens

2014-10-29 14:11 211 查看
<c:forEach>标签用于对一个集合对象中的元素进行循环迭代操作,或者按指定的次数重复迭代执行标签体中的内容。

Item:代表集合,数组,map

Var:代表每次取到的变量。

Entry:对于map,每次获得的是一个entry,entry.key  键   entry.value 值。

Begin :开始 end结束 step步径。

VarStatus:  index 目前下标,count总数,frist是否为首位,last是否为末尾

<h1>实验:遍历10到100的偶数,如果数字所在的位置是3的倍数,显示成红色</h1><hr>
<c:forEach begin="10" end="100" step="2" var="i" varStatus="stat">
<c:if test="${stat.count % 3 == 0}">
<font color="red">
${i }
</font>
</c:if>
<c:if test="${stat.count % 3 != 0}">
<font color="blue">
${i }
</font>
</c:if>
</c:forEach>

<h1>循环执行指定的内容若干次</h1><hr>
<c:forEach begin="0" end="10" step="2" var="i" >
${i },
</c:forEach>
<h1>遍历Map中的数据</h1><hr>
<%
Map map = new LinkedHashMap();
map.put("name","曹操");
map.put("age","59");
map.put("wife","小乔");
map.put("gender","男");
pageContext.setAttribute("map",map);
%>
<c:forEach items="${map}" var="entry" >
${entry.key }:${entry.value }<br>
</c:forEach>

<h1>遍历集合中的数据</h1><hr>
<%
List list = new ArrayList();
list.add("美国");
list.add("中国");
list.add("俄罗斯");
list.add("印度");
list.add("巴西");
pageContext.setAttribute("list",list);
%>
<c:forEach items="${list}" var="c">
${c }<br>
</c:forEach>

<h1>遍历数组中的数据</h1><hr>
<%
String [] city = {"北京","上海","广州","铁岭","葫芦岛"};
pageContext.setAttribute("city",city);
%>
<c:forEach items="${city}" var="c">
${c }<br>
</c:forEach>
<c:forTokens>用来浏览一字符串中所有的成员,其成员是由定义符号所分隔的

按照分隔符切割字符串,切割后相当于一个数组。遍历这个数组去进行操作。      


<c:forTokens items="www.itheima.com" delims="." var="str">
${str }<br>
</c:forTokens>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: