Struts标签中的多层logic:iterator详解(原创)

本文详细探讨了Struts中的logic:iterator标签,揭示了它如何处理不同类型的容器并维护循环索引。重点介绍了name、property、scope和id属性,以及它们在循环遍历集合时的作用。通过示例展示了如何使用该标签生成阶梯状表格,强调了id在多级迭代中的关键作用,以及与bean:write标签配合的注意事项。

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

logic:Iterator标签(以下简称“该标签”)是Struts里非常常用的一个标签,其作用在于循环显示给定容器对象中的值。

如此常用的标签,其源代码当然需要拿出来研究一下,以下列举几条研究成果:
1、该标签内部使用Collection来表示给定的容器,所有的给定容器对象(如ArrayList,Map等)都会被其转化成为Collection,Collection实际就是Map和List的父类。
2、该标签自己维护循环索引,不用程序员管理索引
3、该标签常见的几个属性如下:name、property、scope、id

对应Struts给出的Api说明如下:
name:包括要遍历Collection的Jsp页面的bean的名字(如果property没有被定义),或者是那些通过getter方法获得属性的Jsp中的Bean的名字,这些getter方法返回的是Collection(如果property定义了)。
property:在name命名的Jsp bean中定义的属性的名字,通过getter方法返回一个Collection
scope:指示到哪里去寻找name为名字的bean,如果没有定义缺省为"any scope"
id:如果Collection非空的话,在每次遍历时候Collection中每个元素的名字。


其中除了id每个元素均为Rt expr,这儿的rt expr的意思就是Run Time Expression。明确的说就是,如果你对一个Attribute的<rtexprvalue>指定为true,你就可以在这样的属性中使用<%=%>之类的东东。这个配置文件在tld中。
只有id是必须要说明的。


关于Api说明的说明:
id只是一个临时标识,在下面的<bean:write里面出现的name属性要和id一致才能打印出<bean:write的property,而此property就是在iterator中的属性。

举例说明
以下代码生成一个阶梯状表格
 系统 资源 操作
 soft3     
     res3   
          opt3
 soft12     
     res12   
          opt1211
 soft11     
     res11   
          opt1111

在此之前传来一个request.getAttribute("userPurview"),所以有在第一个logic中的userPurview,就是在这个request里面寻找userPurview
返回的是一个list

<table width="300" border="0">
<tr><td>系统</td>
<td>资源</td>
<td>操作</td>
</tr>
 <logic:iterate id="targetSys" name="userPurview" scope="request">  //这个id可以随便起名,但是要注意下文使用的一致性
 <tr bgcolor="#cccccc"><td height="21" class="unnamed2">
 <bean:write name="targetSys" property="cn"/>        //此处name和上面id保持一致,property就是第一个list里面的元素
 </td>
 <td height="21" class="unnamed2">&nbsp;</td>
 <td height="21" class="unnamed3">&nbsp;</td>
 </tr>
  <logic:iterate id="targetRes" name="targetSys" property="purviewResList">
  <tr><td height="21" class="unnamed2">&nbsp;</td><td  height="21" class="unnamed5">
  <bean:write name="targetRes" property="cn"/>
  </td>
  <td  height="21" class="unnamed6">&nbsp;</td>
  </tr>

   <logic:iterate id="targetOpr" name="targetRes" property="purviewOprList">
   <tr><td height="21" class="unnamed4">&nbsp;</td><td  height="21" class="unnamed4">&nbsp;</td>
   <td  height="21" class="redzi">
   <bean:write property="cn" name="targetOpr"/></td>
   </tr>
   </logic:iterate>

  </logic:iterate>

 </logic:iterate>
</table>


结论:
多级迭代和单层差不多,唯一注意的就是id和<bean:write中的name的对应,上级logic的id与下级logic的name对应,并且取出来的要是个Collection,name和id不一定实际需要这个bean,都是虚拟的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值