在說明s:iterator標簽的使用前,先了解下struts2中的Value Stack。這里參考了webwork中對Value Stack的描述,由于struts2是在webwork的基礎上進行升級的,因此webwork對于Value Stack的表述同樣適用于struts2。在這里不描述Value Stack具體做什么,但有兩點需要注意:
1.一個value stack本質上是一個List;
2.在棧中調用[n]將返回一個從位置n開始的子棧;
對于2舉個例子說明。假定Value Stack包含了[model,action,others],那么
[0] --- 返回 [model,action,others];
[1] --- 返回 [action,others];
[2] --- 返回 [others];
現在將開始介紹s:iterator的一些使用。以下代碼片段均在開發環境eclipse3.4 wtp、tomcat5.5、jdk5上使用struts2.1.6測試通過。
1) 、訪問 days
defined List<String> days ["Monday","Thursday","Friday","Sunday"]
view plaincopy to clipboardprint?
<s:iterator value="days"><s:property /></s:iterator>
<s:iterator value="days"><s:property /></s:iterator>
2) 、使用 top 關鍵字使用(過濾掉Monday)
defined List<String> days ["Monday","Thursday","Friday","Sunday"]
view plaincopy to clipboardprint?
<s:iterator value="days">
<s:if test="top!='Monday'">
<s:property />
</s:if>
</s:iterator>
<s:iterator value="days">
<s:if test="top!='Monday'">
<s:property />
</s:if>
</s:iterator>
top 指代當前迭代元素,可以為對象;
這里的top可用[0].top替代,但不能使用[0]。[0]代表整個棧對象。如果單純調用[0]將會調用其toString()方法輸出對象信息;
3)、使用 last / first 關鍵字使用
defined String[][] aTs = { { "一", "二", "三", "四" },{ "一一", "二二", "三三", "四四"} };
view plaincopy to clipboardprint?
<!--遍歷二維數組,The trick here is to use 'top' as the value for the inner iterator-->
<s:iterator value="aTs" status="of">
<s:if test="#of.last"><br/></s:if>
<s:iterator value="top">
<!--亦可用[0].top替代。如果單純用[0],則會同時打印該處棧對象信息-->
<s:property />
</s:iterator>
</s:iterator>
<!--遍歷二維數組,The trick here is to use 'top' as the value for the inner iterator-->
<s:iterator value="aTs" status="of">
<s:if test="#of.last"><br/></s:if>
<s:iterator value="top">
<!--亦可用[0].top替代。如果單純用[0],則會同時打印該處棧對象信息-->
<s:property />
</s:iterator>
</s:iterator>
iterator 標簽中的status屬性代表當前迭代的位置;
#of.last用于判斷當前是否跌到的最后一個元素;
last返回一個boolean類型;
first 返回一個boolean類型;
4)、使用 odd / even 關鍵字
下面的例子要實現每行輸出顏色不同的效果。
defined List<String> days ["Monday","Thursday","Friday","Sunday"]
view plaincopy to clipboardprint?
<!--奇數行顯示為紅色,偶數行顯示為綠色-->
<s:iterator value="days" status="offset">
<s:else>
<s:if test="#offset.odd==true">
<li style="color: red" mce_style="color: red"><s:property /></li>
</s:if>
<s:else>
<li><s:property /></li>
</s:else>
</s:else>
</s:iterator>
<!--奇數行顯示為紅色,偶數行顯示為綠色-->
<s:iterator value="days" status="offset">
<s:else>
<s:if test="#offset.odd==true">
<li style="color: red" mce_style="color: red"><s:property /></li>
</s:if>
<s:else>
<li><s:property /></li>
</s:else>
</s:else>
</s:iterator>
odd關鍵字用來判斷當前迭代位置是否為奇數行。odd返回boolean類型;
evne關鍵字用來判斷當前迭代位置是否為偶數行。even返回boolean類型
5)、總結下,當聲明iterator的status屬性時,通過#statusName.method 可以使用以下方法:
even : boolean - 如果當前迭代位置是偶數返回true
odd : boolean - 如果當前迭代位置是奇數返回true
count : int - 返回當前迭代位置的計數(從1開始)
index : int - 返回當前迭代位置的編號(從0開始)
first : boolean - 如果當前迭代位置是第一位時返回true
last : boolean - 如果當前迭代位置是最后一位時返回true
modulus(operand : int) : int - 返回當前計數(從1開始)與指定操作數的模數
6)、最后再來看下在iterator中調用value stack的用法。
假定countries是一個List對象,每一個country有一個name屬性和一個citys List對象,并且每一個city也有一個name屬性 。那么我們想要在迭代cities是訪問countries的name屬性就的用如下方式:
view plaincopy to clipboardprint?
<s:iterator value="countries">
<s:iterator value="cities">
<s:property value="name"/>, <s:property value="[1].name"/><br>
</s:iterator>
</s:iterator>
<s:iterator value="countries">
<s:iterator value="cities">
<s:property value="name"/>, <s:property value="[1].name"/><br>
</s:iterator>
</s:iterator>
這里的 <ww:property value="name"/>取的是ctiy.name;<ww:property value="[1].name"/>取得是country.name
<ww:property value="[1].name"/> 等價于 <ww:property value="[1].top.name"/>
we refer to a specific position on the stack: '[1]'. The top of the stack, position 0, contains the current city, pushed on by the inner iterator; position 1 contains the current country, pushed there by the outer iterator.(city處于當前棧,即top或者[0],而[1]指明了外層iterator對象,即country)
'[n]'標記引用開始位置為n的子棧(sub-stack),而不僅僅是位置n處的對象。因此'[0]'代表整個棧,而'[1]'是除top對象外所有的棧元素。
轉載出處:http://blog.csdn.net/oxcow/archive/2009/09/03/4516283.aspx
posted on 2009-11-05 11:00
lameer 閱讀(304)
評論(0) 編輯 收藏 所屬分類:
struts2學習