struts1和struts2迭代器的用法

7 篇文章 0 订阅

原文地址;http://xiongjiajia.iteye.com/blog/1286173

 

注意:cardList resultList为返回的实体List

struts1
Html代码 复制代码 收藏代码
  1. <logic:empty name="cardList"> 
  2.     <table width='690' border='0' cellspacing='0' cellpadding='0'> 
  3.         <tr> 
  4.             <th>卡号</th><th>类型</th><th>金额</th><th>操作时间</th><th>状态</th><th>订单号</th> 
  5.         </tr> 
  6.         <tr><td class='td_center' colspan='5'>无记录</td></tr> 
  7.     </table> 
  8. </logic:empty> 
  9. <logic:notEmpty name="cardList"> 
  10.     <table width='690' border='0' cellspacing='0' cellpadding='0'> 
  11.         <tr> 
  12.             <th>交易时间</th><th>卡号</th><th>类型</th><th>金额</th><th>状态</th><th>订单号</th> 
  13.         </tr> 
  14.     <logic:iterate id="cardList" name="cardList" type="com.yw.hibernate.CardDetail" indexId="rowid"> 
  15.         <tr <%if((rowid+1)%2==1) out.print("bgcolor='#FFF9EB'");%> height="20"> 
  16.             <td class='td_center'> 
  17.                 <bean:write name="cardList" property="create_dt" format="yyyy-MM-dd HH:mm:ss"/> 
  18.             </td> 
  19.             <td class='td_center'><bean:write name="cardList" property="card_no"/></td> 
  20.             <td class='td_center'><bean:write name="cardList" property="o_type"/></td> 
  21.             <td class='td_center'><bean:write name="cardList" property="o_money"/></td> 
  22.             <td class='td_center'><bean:write name="cardList" property="o_state"/></td> 
  23.             <td class='td_center'><bean:write name="cardList" property="orderid"/></td> 
  24.             </tr> 
  25.     </logic:iterate> 
  26.             <tr bgcolor="#FFFFFF" height="20"> 
  27.                 <td class='td_center' colspan="6" ><app:pageList script="goToPage" />&nbsp;</td> 
  28.             </tr> 
  29.     </table> 
  30. </logic:notEmpty> 
<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" />&nbsp;</td>
			</tr>
	</table>
</logic:notEmpty>

struts2
Html代码 复制代码 收藏代码
  1. <s:iterator value="resultList" id="user" status="st"> 
  2.     <tr> 
  3.         <td><input type="checkbox"></td> 
  4.         <td><s:property value="#user.username"/></td> 
  5.         <td><s:property value="#user.password"/></td> 
  6.         <td><s:property value="#user.sex"/></td> 
  7.         <td><s:property value="#user.age"/></td> 
  8.         <td><s:property value="#user.email"/></td> 
  9.         <td><s:property value="#user.address"/></td> 
  10. </tr> 
  11. </s:iterator> 
 	 <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的根对象因此访问需要加上 #访问如下例子:

Html代码 复制代码 收藏代码
  1. <s:iterator value=”{’dd’,'bb’,'cc’}” status=”st”> 
  2.  
  3. <s:if test=”#st.odd”> 
  4.  
  5. <s:property value=”#st.index”/> 
  6.  
  7. </s:if> 
  8.  
  9. </s:iterator> 
<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 就迭代几次,分别使用
Html代码 复制代码 收藏代码
  1. <s:property value=”key”/> 
  2. <s:property value=”value”/> 
<s:property value=”key”/>
<s:property value=”value”/>

输出即可.

案例如下(WebWork标签):实现奇偶行不同颜色显示

JSP中结果集显示如下:

Html代码 复制代码 收藏代码
  1. <s:iterator value="resultList" status="status"> 
  2.  
  3. <tr class="<s:if test="#status.even">row-even</s:if><s:else>row-odd</s:else>"> 
  4.  
  5. <td> 
  6.  
  7. <s:property value=""/> 
  8.  
  9. </td> 
  10.  
  11. </tr> 
  12.  
  13. </s:iterator> 
<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) 当前行数取模

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值