| |
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,都是虚拟的。
|
|