jstl之forEach循环
2013-05-11 14:46
309 查看
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL测试1--c:forEach循环</title>
</head>
<body>
一、整数
<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>
五、对数组进行循环<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]; [转自:51item.net]
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<p>
<c:forEach var="prop" items="${numberMap}" begin="1" end="5">
<c:out value="${prop.key}"/> = <c:out value="${prop.value}"/><br>
</c:forEach>
</body>
</html>
转自http://www.cnblogs.com/anuoruibo/archive/2012/06/13/2547834.html
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL测试1--c:forEach循环</title>
</head>
<body>
一、整数
<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>
五、对数组进行循环<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]; [转自:51item.net]
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<p>
<c:forEach var="prop" items="${numberMap}" begin="1" end="5">
<c:out value="${prop.key}"/> = <c:out value="${prop.value}"/><br>
</c:forEach>
</body>
</html>
转自http://www.cnblogs.com/anuoruibo/archive/2012/06/13/2547834.html
相关文章推荐
- 自定义JSP中的Taglib标签之三简单仿JSTL中带标签体的ForEach循环
- JSTL FOREACH循环每行三列的显示方法
- JSTL之<c:foreach>循环展示table
- java中的JSTL的forEach得到循环变量
- jstl forEach 避免list双层循环
- Taglib自定义标签之三简单仿JSTL中带标签体的ForEach循环
- <c:forEach>跳出循环--JSTL
- JSTL中c:forEach循环里的值的substr操作及对String操作的常用API
- JSTL中c:forEach循环里的值的substr操作及对String操作的常用API
- 【JSTL】--JSTL表达式:c:forEach,varstatus/begin end/循环控制标签--drp215
- jstl之foreach循环
- Jstl Foreach循环Property 'studno' not found on type java.lang.String
- jstl之foreach循环
- 【JSP JSTL】<c:if>多个判断条件 + <c:foreach>满足条件跳出循环
- jstl之forEach循环
- JSTL标签 foreach 退出循环
- JSTL <c:forEach> 跳出循环
- jstl forEach标签动态循环生成tr
- jstl使用foreach 循环遍历输出表格和数据
- jstl之forEach循环