隨筆-2  評(píng)論-0  文章-17  trackbacks-0

          在說(shuō)明s:iterator標(biāo)簽的使用前,先了解下struts2中的Value Stack。這里參考了webwork中對(duì)Value Stack的描述,由于struts2是在webwork的基礎(chǔ)上進(jìn)行升級(jí)的,因此webwork對(duì)于Value Stack的表述同樣適用于struts2。在這里不描述Value Stack具體做什么,但有兩點(diǎn)需要注意:

          1.一個(gè)value stack本質(zhì)上是一個(gè)List;
          2.在棧中調(diào)用[n]將返回一個(gè)從位置n開(kāi)始的子棧;
           

          對(duì)于2舉個(gè)例子說(shuō)明。假定Value Stack包含了[model,action,others],那么

          [0] --- 返回 [model,action,others];
          [1] --- 返回 [action,others];
          [2] --- 返回 [others];
          現(xiàn)在將開(kāi)始介紹s:iterator的一些使用。以下代碼片段均在開(kāi)發(fā)環(huán)境eclipse3.4 wtp、tomcat5.5、jdk5上使用struts2.1.6測(cè)試通過(guò)。

          1) 、訪問(wèn) 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 關(guān)鍵字使用(過(guò)濾掉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 指代當(dāng)前迭代元素,可以為對(duì)象;
          這里的top可用[0].top替代,但不能使用[0]。[0]代表整個(gè)棧對(duì)象。如果單純調(diào)用[0]將會(huì)調(diào)用其toString()方法輸出對(duì)象信息;

          3)、使用 last / first 關(guān)鍵字使用

          defined  String[][] aTs = { { "一", "二", "三", "四" },{ "一一", "二二", "三三", "四四"} };

          view plaincopy to clipboardprint?
          <!--遍歷二維數(shù)組,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],則會(huì)同時(shí)打印該處棧對(duì)象信息-->  
                           <s:property />  
                      </s:iterator>  
                  </s:iterator> 
          <!--遍歷二維數(shù)組,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],則會(huì)同時(shí)打印該處棧對(duì)象信息-->
                     <s:property />
                     </s:iterator>
                 </s:iterator>  
          iterator 標(biāo)簽中的status屬性代表當(dāng)前迭代的位置;
          #of.last用于判斷當(dāng)前是否跌到的最后一個(gè)元素;
          last返回一個(gè)boolean類型;
          first 返回一個(gè)boolean類型;

          4)、使用 odd / even 關(guān)鍵字

          下面的例子要實(shí)現(xiàn)每行輸出顏色不同的效果。

          defined  List<String>  days   ["Monday","Thursday","Friday","Sunday"]

          view plaincopy to clipboardprint?
                 <!--奇數(shù)行顯示為紅色,偶數(shù)行顯示為綠色--> 
                 <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> 
                  <!--奇數(shù)行顯示為紅色,偶數(shù)行顯示為綠色-->
                  <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關(guān)鍵字用來(lái)判斷當(dāng)前迭代位置是否為奇數(shù)行。odd返回boolean類型;
          evne關(guān)鍵字用來(lái)判斷當(dāng)前迭代位置是否為偶數(shù)行。even返回boolean類型

          5)、總結(jié)下,當(dāng)聲明iterator的status屬性時(shí),通過(guò)#statusName.method 可以使用以下方法:

          even : boolean - 如果當(dāng)前迭代位置是偶數(shù)返回true
          odd : boolean - 如果當(dāng)前迭代位置是奇數(shù)返回true
          count : int - 返回當(dāng)前迭代位置的計(jì)數(shù)(從1開(kāi)始)
          index : int - 返回當(dāng)前迭代位置的編號(hào)(從0開(kāi)始)
          first : boolean - 如果當(dāng)前迭代位置是第一位時(shí)返回true
          last : boolean - 如果當(dāng)前迭代位置是最后一位時(shí)返回true
          modulus(operand : int) : int - 返回當(dāng)前計(jì)數(shù)(從1開(kāi)始)與指定操作數(shù)的模數(shù)

          6)、最后再來(lái)看下在iterator中調(diào)用value stack的用法。

          假定countries是一個(gè)List對(duì)象,每一個(gè)country有一個(gè)name屬性和一個(gè)citys List對(duì)象,并且每一個(gè)city也有一個(gè)name屬性 。那么我們想要在迭代cities是訪問(wèn)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"/> 等價(jià)于 <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處于當(dāng)前棧,即top或者[0],而[1]指明了外層iterator對(duì)象,即country)
           '[n]'標(biāo)記引用開(kāi)始位置為n的子棧(sub-stack),而不僅僅是位置n處的對(duì)象。因此'[0]'代表整個(gè)棧,而'[1]'是除top對(duì)象外所有的棧元素。


          轉(zhuǎn)載出處:http://blog.csdn.net/oxcow/archive/2009/09/03/4516283.aspx

          posted on 2009-11-05 11:00 lameer 閱讀(311) 評(píng)論(0)  編輯  收藏 所屬分類: struts2學(xué)習(xí)
          主站蜘蛛池模板: 武川县| 陕西省| 定西市| 抚顺县| 桐乡市| 尚志市| 海口市| 武陟县| 霍城县| 长沙市| 石门县| 两当县| 泸水县| 全州县| 沂南县| 石渠县| 利川市| 揭西县| 醴陵市| 苏州市| 鄂托克旗| 抚顺县| 改则县| 兴宁市| 保德县| 明星| 深圳市| 杨浦区| 岗巴县| 日照市| 黄陵县| 株洲市| 淳化县| 航空| 资溪县| 贵南县| 来安县| 马山县| 剑川县| 油尖旺区| 丰县|