Struts iterators (iterator) traverse List of 4 common examples

Source: Internet
Author: User

1. Traverse list<string>

2. Traverse list<list<string>>

3. Traverse Userentity

4. Traverse list<userentity>

For the detailed code of the example, please refer to Annex 1 for a screenshot of two 4 examples (see Figure 1 below):

Figure 1:4 Examples of traversing a listd run result
keywords: struts tags, iterator iterators,list<string>,list<list<string>>

1. Traverse list<string>

(1) Backstage Testaction.java part code:

         TODO gives the list assignment
         if (list==null) {
             list=new arraylist<string> ();
             List.add ("Liu Bei");
             List.add ("Guan Yu");
          List.add ("Zhang Fei");
            List.add ("Zhao Yun");
         }

(2) Front desk jsp+s tag part source code:

                      <%--List<String> Traverse--%> <table width= "242" height= "" border= "
                              1 "cellpadding=" 1 "cellspacing=" 1 "> <caption>
                              LIST&AMP;LTSTRING&AMP;GT Traversal </caption> <tr>
                               <%--Description: <s:iterator id= "Alias" value= "background list variable name" > <s:if test= "#别名!=null" >--%> <s : iterator id= "li" value= "list" > <td> <s:
                                     If test= "#li!=null" > <s:property/>
                         </s:if> </td> </s:iterator>
</tr>                     </table> <s:iterator value= "STRs" >   
<s:property/><br> </s:iterator> or: <s:iterator value= "STRs" var= "var" > <s:propertyvalue= "Var"/><br> </s:iterator>

Note: Traverse the list<string> run result reference above Figure 1 (upper left)

2. Traverse list<list<string>>

(1) Backstage Testaction.java part code:

//TODO assigns a value to listlist (two-dimensional list) if (listlist==null) {listlist=new arraylist<list& Lt
              String>> ();
              List<string> li=new arraylist<string> ();
              Li.add ("name");
              Li.add ("forces");
              Li.add ("title");
              Listlist.add (LI); 
             /*list is a reference type (one-dimensional list object is saved as an address), adding a different list to a two-dimensional listing needs to be instantiated */Li=new arraylist<string> ();
             Li.add ("Liu Bei");
             Li.add ("Shu");
             Li.add ("eldest brother");
             Listlist.add (LI);
             Li=new arraylist<string> ();
             Li.add ("Guan Yu");
             Li.add ("Shu");
             Li.add ("Old 2"); Listlist.add (LI); 
             Li=new arraylist<string> ();
             Li.add ("Zhang Fei");
             Li.add ("Shu");
             Li.add ("Old 3");
             Listlist.add (LI);
             Li=new arraylist<string> ();
             Li.add ("Zhao Yun");
             Li.add ("Shu");
             Li.add ("Generals");
             Listlist.add (LI);

(2) Front desk jsp+s tag part source code:

     <%--List<List<String>> Traverse--%>
                     <table width= "242" height= "1" border= "1"
                         cellspacing= "1" >
                         <caption>
                             list&ltlist&ltstring&gt&gt traverse as follows:
                         </ caption>
                         <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>
                     </table>

Note: Traverse the list<list<string>> run result reference above Figure 1 (upper right)

3. Traverse userentity

(1) Backstage Testaction.java part code:

TODO gives userentity assignment
  if (userentity==null) {
      userentity=new userentity ("Zhuge Liang", "male", PNS);
  }

(2) Front desk jsp+s tag part source code:

<%--userentity traversal is as follows:--%> < table width = "242" height = "" border = "1" cellpadding = "1" cellspacing = "1" > < caption > userentity traversal as follows: </caption > < tr > <%--< td >< S:property value = "#变量名. Property name "/></TD >--%> < td > < S:property value =" Userentity.name "/> </td > < td > < S:property value = "Userentity.sex"/> </td > < td > < S:property value = "Userentity.age"/> </TD > </tr > </table >

Note: Traverse the userentity run result reference above Figure 1 (bottom left)

4. Traverse list<userentity>

(1) Backstage Testaction.java part code:

  TODO assigns the list<userentity> to the
      if (users==null) {
          users=new arraylist<userentity> ();
          Userentity user=new userentity ("Zhang Fei", "male",);
          Users.add (user);
          User=new userentity ("Reese", "male", +);
          Users.add (user);
          User=new userentity ("Wang Wu", "male",);
          Users.add (user);
          User=new userentity ("Little Joe", "female", +);
          Users.add (user);
     }
View Code

(2) Front desk jsp+s tag part source code:

  <%--list<userentity> traverse the following:--%> <table width= "242" height= "1" border= "Cellpaddi"
                              ng= "1" cellspacing= "1" > <caption>
                              The LIST&AMP;LTUSERENTITY&AMP;GT traversal is as follows: </caption> <tr>
                             <th> name </th>
                             <th> Sex </th>
                         <th> Age </th>
         </tr> <s:iterator id= "U" value= "Users" > <%--
         <s:iterator id= "Name" Value= "collection to Traverse" > Users: list<userentity> users corresponding to the background action class;
                     --%>        <tr> <%--<td><s:property value= "#別名. Entity class Property name"/></td>--% 
                                 > <td> <s:property value= "#u. Name"/>
                                     </td> <td> <s:property value= "#u. Sex"/> </td> &lt ;td> <s:property value= "#u. Age"/> </td&
                             Gt </tr> </s:iterator> </table>

Note: Traverse the list<userentity> run result reference above Figure 1 (bottom right)

5. Comparison of usage

(1) Compare list<string> and Userentity

They traverse the display similarly, but careful friends will find that they have the following main differences:
Difference 1: List<string> traversal requires a iterator iterator such as <s:iterator id= "alias" value= "background list variable name", and does not need the Value property directly with the <s:property/ > get list elements;
Difference 2: Userentity on the contrary, iterator iterators are not required, but the Value property <s:property value= "#变量名. Property name"/> is required to get the list element.

(2) Compare list<list<string>> and list<userentity>

They also have similar traversal displays, and they have the following differences:
Difference 1: List<list<string>> because the list does not have attributes that require the use of the iterator's properties status= "St",
such as:<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.