Iterate主要用來處理在頁面上輸出集合類,集合一般來說是下列之一:
1、 java對象的數組
2、 ArrayList、Vector、HashMap等
具體用法請參考struts文檔,這里不作詳細介紹
現在定義一個class,User.java 把它編譯成User.class
package example;
import java.io.Serializable;
public final class User implements Serializable {
private String name = null;
private String password = null;
public String getName () {
return (this.name);
}
public void setName(String name) {
this.name = name;
}
public String getPassword () {
return (this. password);
}
public void setPassword (String password) {
this. password = password;
}
}
然后在一個struts webapplication中創建一個jsp,例如iterate.jsp
<%@ page language=”java” %>
<%@ page import=”example.*”%>
<%@ taglib uri=”/WEB-INF/struts-bean.tld” prefix=”bean” %>
<%@ taglib uri=”/WEB-INF/struts-logic.tld” prefix=”logic” %>
<%
java.util.ArrayList list = new java.util.ArrayList();
User usera=new User();
usera.setName(”white”);
usera.setPassword(”abcd”);
list.add(usera);
User userb=new User();
userb.setName(”mary”);
userb.setPassword(”hijk”);
list.add(userb);
session.setAttribute(”list”, list);
%>
<html><body><table width=”100%”>
<logic:iterate id=”a” name=”list” type=” example.User “>
<tr><td width=”50%”>
name: <bean:write name=”a” property=”name”/>
<td/><td width=”50%”>
password: <bean:write name=”a” property=”password”/>
</td></tr>
</logic:iterate>
</table></body></html>
將User.class, iterate.jsp放到相應的目錄,運行iterate.jsp你就可以看到iterate的效果了
iterate標記
id 腳本變量的名稱,它保存著集合中當前元素的句柄。
name 代表了你需要疊代的集合,來自session或者request的屬性。
type 是其中的集合類元素的類型
bean的write標記是用來將屬性輸出的,name用來匹配iterate的id,property用來匹配相應類的屬性
<logic:iterate>用法詳解2
2007-04-04 20:34
<login:iterate>標記用于在頁面中創建一個循環,以此來遍歷如數組、Collection、Map這樣的對象。該標記的功能強大,在Struts應用的頁面中經常使用到。
1、對數組進行循環遍歷
使用<logic:iterate>標記可以用于遍歷數組,以下是一段示例代碼:
程序代碼 程序代碼
<%
String[] testArray={”str1″,”str2″,”str3″};
pageContext.setAttribute(”test”,testArray);
%>
<logic:iterate id=”show” name=”test”>
<bean:write name=”show”/>
</logic:iterate>
在上面的代碼中,首先定義了一個字符串數組,并為其初始化。接著,將該數組存入pageContext對象中,命名為test1。然后使用<logic:iterate>標記的name屬性指定了該數組,并使用id來引用它,同時使用<bean:write>標記來將其顯示出來。其結果為:
str1
str2
str3
另外,還可以通過length屬性來指定輸出元素的個數。如下面的代碼:
程序代碼 程序代碼
<logic:iterate id=”show” name=”test” length=”2″ offset=”1″>
<bean:write name=”show”/>
</logic:iterate>
其中length屬性指定了輸出元素的個數,offset屬性指定了從第幾個元素開始輸出,如此處為1,則表示從第二個元素開始輸出。所以該代碼的運行結果應當輸出:
str2
str3
另外,該標記還有一個indexId屬性,它指定一個變量存放當前集合中正被訪問的元素的序號,如:
程序代碼 程序代碼
<logic:iterate id=”show” name=”test” length=”2″ offset=”1″ indexId=”number”>
<bean:write name=”number”/>:<bean:write name=”show”/>
</logic:iterate>
其顯示結果為:
1:str2
2:str3
2、對HashMap進行循環遍歷
程序代碼 程序代碼
<%
HashMap countries=new HashMap();
countries.put(”country1″,”中國”);
countries.put(”country2″,”美國”);
countries.put(”country3″,”英國”);
countries.put(”country4″,”法國”);
countries.put(”country5″,”德國”);
pageContext.setAttribute(”countries”,countries);
%>
<logic:iterate id=”country” name=”countries”>
<bean:write name=”country” property=”key”/>:
<bean:write name=”country” property=”value”/>
</logic:iterate>
在bean:write中通過property的key和value分別獲得HaspMap對象的鍵和值。其顯示結果為:
country5:德國
country3:英國
country2:美國
country4:法國
country1:中國
由結果可看出,它并未按添加的順序將其顯示出來。這是因為HaspMap是無序存放的。
3、嵌套遍歷
程序代碼 程序代碼
<%
String[] colors={”red”,”green”,”blue”};
String[] countries1={”中國”,”美國”,”法國”};
String[] persons={”喬丹”,”布什”,”克林頓”};
ArrayList list2=new ArrayList();
list2.add(colors);
list2.add(countries1);
list2.add(persons);
pageContext.setAttribute(”list2″,list2);
%>
<logic:iterate id=”first” name=”list2″ indexId=”numberfirst”>
<bean:write name=”numberfirst”/>
<logic:iterate id=”second” name=”first”>
<bean:write name=”second”/>
</logic:iterate>
<br>
</logic:iterate>
運行效果:
0 red green blue
1 中國 美國 法國
2 喬丹 布什 克林頓
<logic:iterate id="myBean" indexId="ind" name="循環JavaBean的對象名稱">
<bean:write name="myBean" property="Bean的屬性">
<!--循環Map-->
<logic:iterate id="myHashMap" indexId="mapInd" name="myBean" property="hashMap名稱">
<!--輸出hashMap的鍵,不需要可以不用這行-->
<bean:write name="myHashMap" property="key">
<!--輸出hashMap的值,如果值是個數組或者又一個MAP你可以再對他循環-->
<bean:write name="myHashMap" property="value">
</logic:iterate>
<logic:iterate>
轉自 聽雪樓 http://www.tingxuelou.com/index.php/archives/517