Struts迭代器(iterator)遍歷List經常使用的4種例子 - 馮世傑

【摘要】本文主要介紹及演示了Struts迭代器(iterator)遍歷List經常使用的4種例子,基於MyEclipse開發環境,重點關注先後端代碼的實現,給出後端java代碼、前段struts標籤代碼,主要有以下4個例子:php

1. 遍歷List<String>java

2. 遍歷List<List<String>> 
3. 遍歷UserEntity 
4. 遍歷List<UserEntity>web

例子的詳細代碼請參考附件1,給出兩個4個例子的運行截圖(以下圖1): 
 圖1:遍歷Listd的4個例子運行結果
                   圖1:遍歷Listd的4個例子運行結果 
關鍵詞:Struts標籤,iterator迭代器,List<String>,List<List<String>>後端

一、遍歷List<String>   jsp

(1)後臺TestAction.java 部分代碼: spa

1         // TODO 給list賦值2         if (list==null) {3             list=new ArrayList<String>();4             list.add("劉備");5             list.add("關羽");6             list.add("張飛");7             list.add("趙雲");8         }

TODO 給list賦值code

(2)前臺jsp+s標籤部分源代碼:orm

 1                     <%--List<String>遍歷--%> 2                     <table width="242" height="256" border="1" cellpadding="1" 3                         cellspacing="1"> 4                         <caption> 5                             List&ltString&gt遍歷 6                         </caption> 7                         <tr> 8                             <%-- 9                         說明:<s:iterator id="別名" value="後臺List變量名">10                               <s:if test="#別名!=null">11                             --%>12                             <s:iterator id="li" value="list">13                                 <td>14                                     <s:if test="#li!=null">15                                         <s:property />16                                     </s:if>17                                 </td>18                             </s:iterator>19                         </tr>20                     </table>

List遍歷xml

注:遍歷List<String>運行結果參考上圖1(左上)對象

二、遍歷List<List<String>>

(1)後臺TestAction.java 部分代碼: 

 1         // TODO 給listList賦值(二維列表List) 2         if (listList==null) { 3             listList=new ArrayList<List<String>>(); 4             List<String> li=new ArrayList<String>(); 5             li.add("姓名"); 6             li.add("勢力"); 7             li.add("職務"); 8             listList.add(li); 9             /*List爲引用類型(以地址方式保存一維List對象),10             添加不一樣List到二維列表需再實例化    */        11             li=new ArrayList<String>();12             li.add("劉備");13             li.add("蜀");14             li.add("老大");15             listList.add(li);16             li=new ArrayList<String>();17             li.add("關羽");18             li.add("蜀");19             li.add("老2");20             listList.add(li);21             li=new ArrayList<String>();22             li.add("張飛");23             li.add("蜀");24             li.add("老3");25             listList.add(li);26             li=new ArrayList<String>();27             li.add("趙雲");28             li.add("蜀");29             li.add("大將");30             listList.add(li);

TODO 給listList賦值(二維列表List)

(2)前臺jsp+s標籤部分源代碼:

 1     <%--List<List<String>>遍歷--%> 2                     <table width="242" height="256" border="1" cellpadding="1" 3                         cellspacing="1"> 4                         <caption> 5                             List&ltList&ltString&gt&gt遍歷以下: 6                         </caption> 7                         <s:iterator value="#request.listList" status="st"> 8                             <tr> 9                                 <s:iterator value="#request.listList.get(#st.index)">10                                     <td valign="middle" align="center">11                                         <s:property />12                                     </td>13                                 </s:iterator>14                             </tr>15                         </s:iterator>16                     </table>

View Code

注:遍歷List<List<String>>運行結果參考上圖1(右上)

三、遍歷UserEntity

(1)後臺TestAction.java 部分代碼: 

1 // TODO 給UserEntity賦值2             if (userEntity==null) {3                 userEntity=new UserEntity("諸葛亮","男",37);4             }

TODO 給UserEntity賦值

(2)前臺jsp+s標籤部分源代碼:

 1                     <%--UserEntity遍歷以下:--%> 2                     <table width="242" height="256" border="1" cellpadding="1" 3                         cellspacing="1"> 4                         <caption> 5                             UserEntity遍歷以下: 6                         </caption> 7                         <tr> 8                             <%--<td><s:property value="#變量名.屬性名" /></td>--%> 9                             <td>10                                 <s:property value="userEntity.name" />11                             </td>12                             <td>13                                 <s:property value="userEntity.sex" />14                             </td>15                             <td>16                                 <s:property value="userEntity.age" />17                             </td>18                         </tr>19                     </table>

View Code

注:遍歷UserEntity運行結果參考上圖1(左下)

四、遍歷List<UserEntity>

(1)後臺TestAction.java 部分代碼: 

 1 // TODO 給List<UserEntity>賦值 2     if (users==null) { 3         users=new ArrayList<UserEntity> (); 4         UserEntity user=new UserEntity("張飛","男",18); 5         users.add(user); 6         user=new UserEntity("李斯","男",19); 7         users.add(user); 8         user=new UserEntity("王武","男",20); 9         users.add(user);10         user=new UserEntity("小喬","女",21);11         users.add(user);12     }

View Code

(2)前臺jsp+s標籤部分源代碼:

 1 <%-- List<UserEntity>遍歷以下:--%> 2                     <table width="242" height="256" border="1" cellpadding="1" 3                         cellspacing="1"> 4                         <caption> 5                             List&ltUserEntity&gt遍歷以下: 6                         </caption> 7                         <tr> 8                             <th> 9                                 姓名10                             </th>11                             <th>12                                 性別13                             </th>14                             <th>15                                 年齡16                             </th>17                         </tr>18                         <s:iterator id="u" value="users">19                             <%--20         <s:iterator  id="別名"  value="要遍歷的集合">21         users:對應後臺Action類的List<UserEntity> users;22         --%>23                             <tr>24                                 <%--<td><s:property value="#別名.實體類屬性名" /></td>--%>25                                 <td>26                                     <s:property value="#u.name" />27                                 </td>28                                 <td>29                                     <s:property value="#u.sex" />30                                 </td>31                                 <td>32                                     <s:property value="#u.age" />33                                 </td>34                             </tr>35                         </s:iterator>36                     </table>

View Code

注:遍歷List<UserEntity>運行結果參考上圖1(右下)

五、用法比較

(1)比較List<String>和UserEntity

  它們遍歷顯示效果相似,可是細心的朋友會發現,它們主要有如下區別: 
  區別1: List<String>的遍歷須要iterator迭代器如<s:iterator id="別名" value="後臺List變量名">,且不須要value屬性直接用<s:property />獲取列表元素; 
  區別2: UserEntity剛相反,不須要iterator迭代器,但須要value屬性<s:property value="#變量名.屬性名" />來獲取列表元素。

(2)比較List<List<String>>和List<UserEntity>

  它們遍歷顯示效果也相似,它們也有如下區別: 
  區別1: List<List<String>>因爲列表沒有屬性須要藉助迭代器的屬性status="st", 
  如:

<s:iterator value="#request.listList" status="st">
  <tr>
    <s:iterator value="#request.listList.get(#st.index)">
        <td valign="middle" align="center">
          <s:property />
        </td></s:iterator></tr></s:iterator>

   區別2: List<UserEntity>則簡單多,借用迭代器iterator的id、value屬性,再用<s:property value="#別名.實體類屬性名" />獲取

  如:

<s:iterator id="u" value="users">
   <tr>
       <td>
         <s:property value="#u.name" />
       </td>
       <td>
         <s:property value="#u.sex" />
       </td>
       <td>
         <s:property value="#u.age" />
       </td>
   </tr></s:iterator>

  綜合以上的對比分析: 遍歷方法各有長短,若 不是多表聯接查詢 、顯示錶格的 列數已經固定  通常用實體類 (UserEntity)迭代的方式。相反,顯示的 數據表格行和列不肯定 ,此時用實體類(UserEntity)迭代的方式很難實現, 只能用List<String>或List<List<String>>實現了。

相關文章
相關標籤/搜索