JSTL forEach

JSTL forEach的用法(2010-06-14 16:29:26)轉載標籤: 雜談
<c:forEach>標籤的語法定義如下所示。

<c:forEach var="name" items="expression" varStatus="name"

begin="expression" end="expression" step="expression">

body content

</c:forEach>

<c:forEach>標籤具有以下一些屬性:

l var:迭代參數的名稱。在迭代體中可以使用的變量的名稱,用來表示每一個迭代變量。類型爲String。

l items:要進行迭代的集合。對於它所支持的類型將在下面進行講解。

l varStatus:迭代變量的名稱,用來表示迭代的狀態,可以訪問到迭代自身的信息。

l begin:如果指定了items,那麼迭代就從items[begin]開始進行迭代;如果沒有指定items,那麼就從begin開始迭代。它的類型爲整數。

l end:如果指定了items,那麼就在items[end]結束迭代;如果沒有指定items,那麼就在end結束迭代。它的類型也爲整數。

l step:迭代的步長。

<c:forEach>標籤的items屬性支持Java平臺所提供的所有標準集合類型。此外,您可以使用該操作來迭代數組(包括基本類型數組)中的元素。它所支持的集合類型以及迭代的元素如下所示:

l java.util.Collection:調用iterator()來獲得的元素。

l java.util.Map:通過java.util.Map.Entry所獲得的實例。

l java.util.Iterator:迭代器元素。

l java.util.Enumeration:枚舉元素。

l Object實例數組:數組元素。

l 基本類型值數組:經過包裝的數組元素。

l 用逗號定界的String:分割後的子字符串。

l javax.servlet.jsp.jstl.sql.Result:SQL查詢所獲得的行。

不論是對整數還是對集合進行迭代, <c:forEach>的varStatus屬性所起的作用相同。和var屬性一樣,varStatus用於創建限定了作用域的變量(改變量只在當前標籤體內起作用)。不過,由varStatus屬性命名的變量並不存儲當前索引值或當前元素,而是賦予javax.servlet.jsp.jstl.core.LoopTagStatus類的實例。該類包含了一系列的特性,它們描述了迭代的當前狀態,如下這些屬性的含義如下所示:

l current:當前這次迭代的(集合中的)項。

l index:當前這次迭代從0開始的迭代索引。

l count:當前這次迭代從1開始的迭代計數。

l first:用來表明當前這輪迭代是否爲第一次迭代,該屬性爲boolean類型。

l last:用來表明當前這輪迭代是否爲最後一次迭代,該屬性爲boolean類型。

l begin:begin屬性的值。

l end:end屬性的值

l step:step屬性的值

下面就來看一個個基本的例子,表格隔行背景色變化



<%@ 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>



本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/jianpingdu2009/archive/2010/05/17/5600738.aspx
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章