struts2中s:iterator 标签

struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:


s:iterator 标签有3个属性:
    value:被迭代的集合
    id   :指定集合里面的元素的id
    status 迭代元素的索引


1:jsp页面定义元素写法 数组或list

<s:iterator value="{'1','2','3','4','5'}" id='number'>
    <s:property value='number'/>A
</s:iterator>
打印结果为: 1A2A3A4A5A


2:索引的用法


如果指定了status,每次的迭代数据都有IteratorStatus的实例,它有以下几个方法


int getCount()返回当前迭代了几个元素
int getIndex()返回当前元素索引
boolean isEven()当然的索引是否偶数
boolean isFirst()当前是否第一个元素
boolean isLast()
boolean isOdd()当前元素索引是否奇数


<s:iterator value="{'a','b','c'}" id='char' status='st'>
    <s:if test="#st.Even">
        现在的索引是奇数为:<s:property value='#st.index'/>
    </s:if>
    当前元素值:<s:property value='char'/>
</s:iterator>


3:遍历map


value可以直接定义为:
value="#{"1":"a","2":"b"}"
每个元素以都好隔开。元素之间的key和value 冒号隔开
value也可以是数据栈里面的java.util.Map对象


遍历写法如下:
<s:iterator value="map" id="id" status="st">
     key : <s:property value='key'/>
     value:<s:property vlaue='value'/>
</s:iterator>


当然key 和value 都可以使java 的 Object


4:遍历数据栈.简单的List类


List<Attr>
class Attr{String attrName;String getAttrName(){return "123";}}


<s:iterator value="label" id="id">
    <s:property value="#id.attrName" />
</s:iterator>


当然value 还可以写成 value="%{label}" label可以有.操作
label的属性List 可以写成value="%{label.list}" 相当于:getLabel().getList();


5:遍历2个list;


List<AttrName> attrN {color,size,style}
List<AttrValue> attrV {red,20,gay}
这2个list的元素是一一对应的,一个attrN对应一个attrV


<s:iterator value="%{attrN }" id="id"   status="status">
index    is : <s:property value='status.index'/>
attrName is : <s:property value='id'/> or <s:property value='%{id}'/>
attrName is : <s:property value='%{attrV[#status.index]}'/>
</s:iterator>

<s:bean name="org.apache.struts2.util.Counter" id="counter">
   <s:param name="first" value="5" />
   <s:param name="last" value="10" />
   <s:iterator>
     counter:<s:property/>
   </s:iterator>
</s:bean>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/quxiuer/archive/2009/09/15/4553667.aspx

()

Examples

The following example retrieves the value of the getDays() method of the current object on the value stack and uses it to iterate over. The <s:property/> tag prints out the current value of the iterator.

<s:iterator value="days">
  <p>day is: <s:property/></p>
</s:iterator>

The following example uses a Bean tag and places it into the ActionContext. The iterator tag will retrieve that object from the ActionContext and then calls its getDays() method as above. The status attribute is also used to create an IteratorStatus object, which in this example, its odd() method is used to alternate row colours:

<s:bean name="org.apache.struts2.example.IteratorExample" var="it">
  <s:param name="day" value="'foo'"/>
  <s:param name="day" value="'bar'"/>
</s:bean>
<p/>
<table border="0" cellspacing="0" cellpadding="1">
<tr>
  <th>Days of the week</th>
</tr>
<p/>
<s:iterator value="#it.days" status="rowstatus">
  <tr>
    <s:if test="#rowstatus.odd == true">
      <td style="background: grey"><s:property/></td>
    </s:if>
    <s:else>
      <td><s:property/></td>
    </s:else>
  </tr>
</s:iterator>
</table>

The next example will further demonstrate the use of the status attribute, using a DAO obtained from the action class through OGNL, iterating over groups and their users (in a security context). The last() method indicates if the current object is the last available in the iteration, and if not, we need to separate the users using a comma:

 <s:iterator value="groupDao.groups" status="groupStatus">
     <tr class="<s:if test="#groupStatus.odd == true ">odd</s:if><s:else>even</s:else>">
         <td><s:property value="name" /></td>
         <td><s:property value="description" /></td>
         <td>
             <s:iterator value="users" status="userStatus">
                 <s:property value="fullName" /><s:if test="!#userStatus.last">,</s:if>
             </s:iterator>
         </td>
     </tr>
 </s:iterator>

The next example iterates over a an action collection and passes every iterator value to another action. The trick here lies in the use of the '[0]' operator. It takes the current iterator value and passes it on to the edit action. Using the '[0]' operator has the same effect as using <s:property />. (The latter, however, does not work from inside the param tag).

     <s:action name="entries" var="entries"/>
     <s:iterator value="#entries.entries" >
         <s:property value="name" />
         <s:property />
         <s:push value="...">
             <s:action name="edit" var="edit" >
                 <s:param name="entry" value="[0]" />
             </s:action>
         </push>
     </s:iterator>

A loop that iterates 5 times

<s:iterator var="counter" begin="1" end="5" >
   <!-- current iteration value (1, ... 5) -->
   <s:property value="top" />
</s:iterator>

Another way to create a simple loop, similar to JSTL's <c:forEach begin="..." end="..." ...> is to use some OGNL magic, which provides some under-the-covers magic to make 0-n loops trivial. This example also loops five times.

<s:iterator status="stat" value="(5).{ #this }" >
   <s:property value="#stat.count" /> <!-- Note that "count" is 1-based, "index" is 0-based. -->
</s:iterator>

A loop that iterates over a partial list

<s:iterator value="{1,2,3,4,5}" begin="2" end="4" >
   <!-- current iteration value (2,3,4) -->
   <s:property value="top" />
</s:iterator>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值