Struts2標簽實現for循環


感悟:但是不建議使用這種方法,按照MVC框架的思想 ,應該把業務更多放在后台。前台盡量只進行數據展示。

 

轉自:http://blog.csdn.net/guandajian/article/details/7334756

 

在struts2及webwork中要實現如:

1 for(int i=0;i<10;i++){
2        //內容  
3 }

 

還是需要一些技巧的,我在做分頁條的時候,要輸出頁碼,怪了,用迭代器不行的,看了一下struts2的文檔及例子也沒發現用計數器的地方,偶然看了一下bea標簽,哦,原來如此....

1 <s:bean name="org.apache.struts2.util.Counter" id="counter">
2    <s:param name="first" value="1" />  //起始值
3    <s:param name="last" value="10" />  //循環控制變量
4    <s:iterator>
5      counter:<s:property/>   //內容
6    </s:iterator>
7 </s:bean>

 

其中first屬性指定循環起始值,last指定循環終止值,其它相關屬性可以查看org.apache.struts2.util.Counter類源碼。在下面迭代器中輸入循環的當前值,即:current

干脆把源碼貼出來吧!

-----------------------------------------------------------------------------------------------------------------------------

  1 package org.apache.struts2.util;
  2 
  3 import java.io.Serializable;
  4 
  5 
  6 /**
  7 * A bean that can be used to keep track of a counter.
  8 * <p/>
  9 * Since it is an Iterator it can be used by the iterator tag
 10 *
 11 */
 12 public class Counter implements java.util.Iterator, Serializable {
 13 
 14      private static final long serialVersionUID = 2796965884308060179L;
 15 
 16      boolean wrap = false;
 17 
 18      // Attributes ----------------------------------------------------
 19      long first = 1;
 20      long current = first;
 21      long interval = 1;
 22      long last = -1;
 23 
 24 
 25      public void setAdd(long addition) {
 26          current += addition;
 27      }
 28 
 29      public void setCurrent(long current) {
 30          this.current = current;
 31      }
 32 
 33      public long getCurrent() {
 34          return current;
 35      }
 36 
 37      public void setFirst(long first) {
 38          this.first = first;
 39          current = first;
 40      }
 41 
 42      public long getFirst() {
 43          return first;
 44      }
 45 
 46      public void setInterval(long interval) {
 47          this.interval = interval;
 48      }
 49 
 50      public long getInterval() {
 51          return interval;
 52      }
 53 
 54      public void setLast(long last) {
 55          this.last = last;
 56      }
 57 
 58      public long getLast() {
 59          return last;
 60      }
 61 
 62      // Public --------------------------------------------------------
 63      public long getNext() {
 64          long next = current;
 65          current += interval;
 66 
 67          if (wrap && (current > last)) {
 68              current -= ((1 + last) - first);
 69          }
 70 
 71          return next;
 72      }
 73 
 74      public long getPrevious() {
 75          current -= interval;
 76 
 77          if (wrap && (current < first)) {
 78              current += (last - first + 1);
 79          }
 80 
 81          return current;
 82      }
 83 
 84      public void setWrap(boolean wrap) {
 85          this.wrap = wrap;
 86      }
 87 
 88      public boolean isWrap() {
 89          return wrap;
 90      }
 91 
 92      public boolean hasNext() {
 93          return ((last == -1) || wrap) ? true : (current <= last);
 94      }
 95 
 96      public Object next() {
 97          return new Long(getNext());
 98      }
 99 
100      public void remove() {
101          // Do nothing
102 
103   }
104 
105 }

 

    

 

//項目中的應用

<s:bean name="org.apache.struts2.util.Counter" id="counter">
      <s:param name="first" value="1" />
      <s:param name="last" value="#request.num" />
      <s:iterator>
                    <tr bgcolor="ffffff" align="center">
                        <td>${rs.f_courseId}&nbsp;</td>
                        <td>${rs.f_coursename}&nbsp;</td>
                        <td>${rs.f_term}&nbsp;</td>
                        <td>${rs.f_week}&nbsp;</td>
                        <td>${rs.f_credit}&nbsp;</td>
                        <td>${rs.f_courseStart}&nbsp;</td>
                        <td>${rs.f_courseEnd}&nbsp;</td>
                        <td><a href="">查看</a> <a href="">刪除</a></td>
                    </tr>
      </s:iterator>
</s:bean>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM