Struts标签中的多层logic:iterator详解

王朝other·作者佚名  2008-05-19
宽屏版  字体: |||超大  

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"</td

<td height="21" class="unnamed3"</td

</tr

<logic:iterate id="targetRes" name="targetSys" property="purviewResList"

<tr<td height="21" class="unnamed2"</td<td

height="21" class="unnamed5"

<bean:write name="targetRes" property="cn"/

</td

<td

height="21" class="unnamed6"</td

</tr

<logic:iterate id="targetOpr" name="targetRes" property="purviewOprList"

<tr<td height="21" class="unnamed4"</td<td

height="21" class="unnamed4"</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,都是虚拟的。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
>>返回首页<<
推荐阅读
 
 
频道精选
© 2005- 王朝网络 版权所有