原文地址;http://xiongjiajia.iteye.com/blog/1286173
struts1
- <logic:empty
name="cardList">
- <table
width='690'
border='0'
cellspacing='0'
cellpadding='0'>
- <tr>
- <th>卡号</th><th>类型</th><th>金额</th><th>操作时间</th><th>状态</th><th>订单号</th>
- </tr>
- <tr><td
class='td_center'
colspan='5'>无记录</td></tr>
- </table>
- </logic:empty>
- <logic:notEmpty
name="cardList">
- <table
width='690'
border='0'
cellspacing='0'
cellpadding='0'>
- <tr>
- <th>交易时间</th><th>卡号</th><th>类型</th><th>金额</th><th>状态</th><th>订单号</th>
- </tr>
- <logic:iterate
id="cardList"
name="cardList"
type="com.yw.hibernate.CardDetail"
indexId="rowid">
- <tr
<%if((rowid+1)%2==1) out.print("bgcolor='#FFF9EB'");%>
height="20">
- <td
class='td_center'>
- <bean:write
name="cardList"
property="create_dt"
format="yyyy-MM-dd HH:mm:ss"/>
- </td>
- <td
class='td_center'><bean:write
name="cardList"
property="card_no"/></td>
- <td
class='td_center'><bean:write
name="cardList"
property="o_type"/></td>
- <td
class='td_center'><bean:write
name="cardList"
property="o_money"/></td>
- <td
class='td_center'><bean:write
name="cardList"
property="o_state"/></td>
- <td
class='td_center'><bean:write
name="cardList"
property="orderid"/></td>
- </tr>
- </logic:iterate>
- <tr
bgcolor="#FFFFFF"
height="20">
- <td
class='td_center'
colspan="6"
><app:pageList
script="goToPage"
/> </td>
- </tr>
- </table>
- </logic:notEmpty>
struts2
- <s:iterator
value="resultList"
id="user"
status="st">
- <tr>
- <td><input
type="checkbox"></td>
- <td><s:property
value="#user.username"/></td>
- <td><s:property
value="#user.password"/></td>
- <td><s:property
value="#user.sex"/></td>
- <td><s:property
value="#user.age"/></td>
- <td><s:property
value="#user.email"/></td>
- <td><s:property
value="#user.address"/></td>
- </tr>
- </s:iterator>
1. value属性:可选的属性,value属性是指一个被迭代的集合,使用ognl表达式指定,如果为空的话默认就是ValueStack栈顶的集合.
2.id属性:可选属性, 是指集合元素的id
3.status属性:可选属性,该属性在迭代时会产生一个IteratorStatus对象,该对象可以判断当前元素的位置,包含了以下属性方法:
int getCount(); 迭代元素个数
int getIndex(); 迭代元素当前索引
boolean getFirst(); 是否为第一个
boolean getEven(); 是否为偶
boolean getLast(); 是否最后一个
bolean getOdd(); 是否为奇
由于iteratorstatus对象并不是ognl的根对象因此访问需要加上 #访问如下例子:
- <s:iterator
value=”{’dd’,'bb’,'cc’}”
status=”st”>
- <s:if
test=”#st.odd”>
- <s:property
value=”#st.index”/>
- </s:if>
- </s:iterator>
此外:iterator也可以迭代map对象,map对象中有几对key-value 就迭代几次,分别使用
- <s:property
value=”key”/>
- <s:property
value=”value”/>
输出即可.
案例如下(WebWork标签):实现奇偶行不同颜色显示
JSP中结果集显示如下:
- <s:iterator
value="resultList"
status="status">
- <tr
class="<s:if test="#status.even">row-even</s:if><s:else>row-odd</s:else>">
- <td>
- <s:property
value=""/>
- </td>
- </tr>
- </s:iterator>
样式表可以参考:
.row-even{background-color:#def7c2;}
.row-odd{background-color:#c8e7a6;}
除了使用#status.even判断是否偶数行外,还有其他几个属性可以使用:
#status.odd 是否奇数行
#status.count 当前行数
#status.index 当前行的序号,从0开始[#status.count=#status.index+1]
#status.first 是否第一行
#status.last 是否最后一行
#status.modules(int) 当前行数取模