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>

 

你可能感兴趣的:(struts2标签)