進行循環遍歷的Logic標籤
<logic:iterate>是Logic標籤庫中最複雜的標籤,它能夠在一個循環中遍歷數組,Collection,
Enumeration,Iterator或Map中的所有元素。
1、遍歷集合
<logic:iterate>的name屬性指定需要進行遍歷的集合對象,它每次從集合中檢索出一個元素,然後
把它存放在page範圍內,並以id屬性指定的字符串來命名這個元素,例如:
<%
Vector animals=new Vector();
animals.addElement("Dog");
animals.addElement("Cat");
animals.addElement("Bird");
animals.addElement("Chick");
request.setAttribute("Animals", animals);
%>
<logic:iterate id="element" name="Animals">
<bean:write name="element"/><BR>
</logic:iterate><p>
以上代碼的輸出內容爲:
Dog
Cat
Bird
Chick
length屬性指定需要遍歷的元素的數目,如果沒有設置length屬性,就遍歷集合中的所有元素,offset屬
性指定開始遍歷的起始位置,默認值“0”,表示從一個集合的第一個元素開始遍歷,indexId屬性定義一
個代表當前被遍歷元素序號的變量,這個變量被存放在page範圍內,可以被標籤主體的<bean:write>標籤
訪問,例如:
<logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
<bean:write name="index"/>.<bean:write name="element"/><BR>
</logic:iterate><p>
以上標籤的length屬性爲2,表示只需要遍歷Animals集合中的兩個元素,offset爲1,表示從第二個元
素開始遍歷,輸出內容爲:
1、Cat
2、Bird
2、遍歷Map
<logic:iterate>標籤還可以遍歷HashMap中的元素,例如:
<%
HashMap months = new HashMap();
months.put("Jan.", "January");
months.put("Feb.", "February");
months.put("Mar.", "March");
request.setAttribute("months", months);
%>
<%--
The following section shows iterate.
--%>
<logic:iterate id="element" indexId="ind" name="months">
<bean:write name="ind"/>.
<bean:write name="element" property="key"/>:
<bean:write name="element" property="value"/><BR>
</logic:iterate><P>
標籤遍歷months對象的每一個元素,每一個元素都包含一對key/value,以上代碼輸出:
0、Mar:March
1、Feb:Feberuary
2、Jan:January
如果HashMap中的每個元素的value是集合對象,則可以採用嵌套的<logic:iterate>標籤遍歷集合中的所
有對象,例如:
<%
HashMap h = new HashMap();
String vegetables[] = {"pepper", "cucumber"};
String fruits[] = {"apple","orange","banana","cherry","watermelon"};
String flowers[] = {"chrysanthemum","rose"};
String trees[]={"willow"};
h.put("Vegetables", vegetables);
h.put("Fruits", fruits);
h.put("Flowers", flowers);
h.put("Trees",trees);
request.setAttribute("catalog", h);
%>
<%--
The following section shows iterate.
--%>
<logic:iterate id="element" indexId="ind" name="catalog">
<bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
<logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
-----<bean:write name="elementValue"/><BR>
</logic:iterate>
</logic:iterate><P>
以上代碼下定義了一個名爲'catalog'的HashMap,存放在request範圍,它的每個元素的value爲字符串數
組,接下來外層的標籤遍歷HashMap中的所有元素,內層的標籤訪問每個元素的value屬性,遍歷value屬
性引用的字符串數組中的所有元素,輸出內容爲:
0. Vegetables
----cucumber
1、Flowers
----rose
2.Fruits
----orange
----banana
----cherry
3.Trees
3、設置被遍歷的變量
可以通過以下方式來設置需要遍歷的變量。
a、設置name屬性,name屬性指定需要遍歷的集合或Map,例如:
<logic:iterate id="element" name="Animals">
<bean:write name="element"/><BR>
</logic:iterate><p>
b、設置name屬性和property屬性,name屬性指定一個JavaBean,property屬性指定JavaBean的一個屬性
,這個屬性爲需要遍歷的集合或Map,例如:
<logic:iterate id="element" indexId="ind" name="catalog">
<bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
<logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
-----<bean:write name="elementValue"/><BR>
</logic:iterate>
</logic:iterate><P>
c、設置collection屬性,collection屬性指定一個運行時表達式,表達式的運算結果爲需要遍歷的集合
或Map,例如:
<logic:iterate id="header" collection="<%= request.getHeaderNames() %>">
<bean:write name="header"/><BR>
</logic:iterate>
在logic:iterate嵌入html:link標籤:
<logic:iterate id="display" name="result">
<html:link paramId="id" paramProperty="wzxw" paramName="display">
<bean:write name="display" property="wzxw" />
</html:link>
</logic:iterate>
paramId="id" 是指在 url 中的id http://localhost/test.jsp?id=xxx ,如果 paramId="name",那麼 url 中應該是 http://localhost/test.jsp?name=xxx
paramProperty="wzxw" 是你在 result 中的屬性;
paramName="display" 是 logic:iterate 的 id
進行請求轉發或重定向的Logic標籤
1、<logic:forward>標籤
該標籤用於請求轉發,它的name屬性指定轉發目標,於Struts配置文件中的<global-forwards>元素中的
子元素<forward>匹配,例如,在配置文件中:
<global-forwards>
<forward name="index" path="/index.jsp"/>
</global-forwards>
這樣jsp中通過<logic:forward>標籤把請求轉發給name屬性爲“index”的全局轉發目標:
<logic:forward name ="index"/>
這樣當瀏覽器訪問標籤所在jsp時,標籤把請求轉發給index.jsp,因此瀏覽器看到的是index.jsp
2、<logic:redirect>標籤
該標籤用於重定向,它的forward,href和page屬性指定重定向目標,這幾個屬性的用法和<html:link>的
forward,href,page屬性類似,例如:
<logic:redirect href="http://www.apache.org"/>
這樣瀏覽器訪問標籤所在jsp時,標籤把請求重定向到href屬性內容,因此瀏覽器看到的是
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/kucool/archive/2007/04/13/1563294.aspx