struts tag iterator高级用法

本文探讨了Struts框架中使用迭代元素进行数据展示的方法,包括如何获取元素的索引、判断元素位置、计数及操作迭代状态。通过实例展示了如何在表格中展示分组数据,并对不同元素应用样式,最后介绍了如何遍历集合并执行特定操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Name Return Type Description
index int zero-based index value.
count int index + 1
first boolean returns true if it is the first element
last boolean returns true if it is the last element
even boolean returns true if the count is an even number.
odd boolean returns true if the count is an odd number.
modulus int takes an int value and returns the modulus of count.

如果指定了status,每次的迭代数据都有IteratorStatus的实例,它有以下几个方法
int getCount()返回当前迭代了几个元素
int getIndex()返回当前元素索引
boolean isEven()当然的索引是否偶数
boolean isFirst()当前是否第一个元素
boolean isLast()
boolean isOdd()当前元素索引是否奇数



<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>



<s:bean name="org.apache.struts2.example.IteratorExample" id="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>




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




<s:iterator status="stat" value="{1,2,3,4,5}" >
<!-- grab the index (start with 0 ... ) -->
<s:property value="#stat.index" />

<!-- grab the top of the stack which should be the -->
<!-- current iteration value (0, 1, ... 5) -->
<s:property value="top" />
</s:iterator>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值