)struts2中iterator標籤的相關使用

來源:互聯網
上載者:User

在說明s:iterator標籤的使用前,先瞭解下struts2中的Value Stack。這裡參考了webwork中對Value Stack的描述,由於struts2是在webwork的基礎上進行升級的,因此webwork對於Value Stack的表述同樣適用於struts2。在這裡不描述Value Stack具體做什麼,但有兩點需要注意:

 一個value stack本質上是一個List;
在棧中調用[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對象外所有的棧元素。

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/oxcow/archive/2009/09/03/4516283.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.