s:iterator 迭代 一行兩列

來源:互聯網
上載者:User

假如有jsp頁面要實現一個列表資訊,格式如下:
第1條資訊 第2條資訊
第3條資訊 第4條資訊
第5條資訊 第6條資訊
第7條資訊 第8條資訊

搜尋過別的方法,很多人運用jsp代碼寫入頁面來進行迴圈判斷。其實用struts2內建的標籤可以實現同樣效果,利用iterator標籤進行迴圈,裡面用if標籤判斷何時加入tr標籤的開頭和結尾,下面實現了兩列資訊列表的情況:

  1. <table width="540" border="0" cellspacing="0" cellpadding="0">      
  2.        <s:iterator value="popList" status="count">      
  3.      
  4.          <s:if test="#count.odd||#cout.first">      
  5.               <tr>          
  6.          </s:if>      
  7.      
  8.        <td width="200" height="20">      
  9.          <a href="listPop.action?oid=<s:property value="uid" />" target="_blank">  
  10. <s:property value="code"/></a>  
  11.        </td>  
  12.         <s:if test="#count.even||#count.last">      
  13.             </tr>          
  14.         </s:if>      
  15.         </s:iterator>      
  16. </table>     

因為iterator標籤有如下方法:
boolean isEven() 當前迭代元素索引是否為偶數;
boolean isOdd() 當前迭代元素索引是否為奇數;
boolean isLast() 是否為最後一條元素;
boolean isFirst() 是否為第一條元素。
有以上元素,那麼判斷何時加入tr就非常方便。

假如是多列(兩列以上),那麼我們就可以使用如下方法:
int getIndex() 返回當前迭代元素的索引
用它來計算你何時需要加入tr標籤的開頭和結尾,無非就是if判斷的運算式複雜一些,方法還是一樣的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.