標記用於在頁面中建立乙個迴圈,以此來遍歷如陣列、collection、map這樣的物件。該標記的功能強大,在struts應用的頁面中經常使用到。
1、對陣列進行迴圈遍歷
使用<logic:iterate>標記可以用於遍歷陣列,以下是一段示例**:
<%
string testarray=;
pagecontext.setattribute("test",testarray);
%>
<logic:iterateid="show" name="test">
在上面的**中,首先定義了乙個字串陣列,並為其初始化。接著,將該陣列存入pagecontext物件中,命名為test1。然後使用<logic:iterate>標記的name屬性指定了該陣列,並使用id來引用它,同時使用標記來將其顯示出來。其結果為:
str1
str2
str3
另外,還可以通過length屬性來指定輸出元素的個數。如下面的**:
<logic:iterateid="show" name="test" length="2" offset="1">
其中length屬性指定了輸出元素的個數,offset屬性指定了從第幾個元素開始輸出,如此處為1,則表示從第二個元素開始輸出。所以該**的執行結果應當輸出:
str2
str3
另外,該標記還有乙個indexid屬性,它指定乙個變數存放當前集合中正被訪問的元素的序號,如:
<logic:iterateid="show" name="test" length="2" offset="1" indexid="number"> :
其顯示結果為:
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:iterateid="country" name="countries"> :
在bean:write中通過property的key和value分別獲得haspmap物件的鍵和值。其顯示結果為:
country5:德國
country3:英國
country2:美國
country4:法國
country1:中國
由結果可看出,它並未按新增的順序將其顯示出來。這是因為haspmap是無序存放的。
3、巢狀遍歷
<%
string colors=;
string countries1=;
string persons=;
arraylist list2=new arraylist();
list2.add(colors);
list2.add(countries1);
list2.add(persons);
pagecontext.setattribute("list2",list2);
%>
<logic:iterateid="first" name="list2" indexid="numberfirst">
<logic:iterateid="second" name="first">
執行效果:
0 red green blue
1 中國 美國 法國
2 喬丹 布希 柯林頓
list 是你要遍例的物件 it是list裡面的元素的型別 ,還有個offset屬性,是用來表示起始位置的,比如,offset=「1」從第二位開始遍歷出值,offset=「0」是預設值
Struts標籤使用
1.1.頁面跳轉 可以使用href page action三個屬性實現struts檢視層元件的跳轉,三個屬性的作用及區別為 href使用該網域名稱下的相對路徑,路徑前不需要加 或鏈結至以http開頭其它網域名稱,href index.jsp href http page使用該網域名稱下的絕對路徑,路...
struts的if標籤使用
下面總結一下struts2 中if標籤的使用 1 判斷字串是否為空 s if test user.username null or user.username s if test ab123 null ab123 is null s if s else ab123 not null s else 2...
struts標籤使用示例
1.通過標籤進行判斷 使用2.標籤進行遍歷 修改 刪除3.this.addactionerror 輸入了驗證碼錯誤 addactionerror 錯誤內容 action級別的錯誤訊息 this.addactionerror 錯誤資訊1 this.addactionerror 錯誤資訊2 顯示訊息的標...