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>
<!---
第一級迭代
-->
//request
中的數值為”
userPurview
”作用范圍在
request
中
,
取的
ID
名為
targetSys
<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 name="targetOpr"? property="cn"/></td>
</tr>
</logic:iterate>
</logic:iterate>
</logic:iterate>
</table>
結論:
多級迭代和單層差不多,唯一注意的就是
id
和
<bean:write
中的
name
的對應,上級
logic
的
id
與下級
logic
的
name
對應,并且取出來的要是個
Collection,name
和
id
不一定實際需要這個
bean
,都是虛擬的。