Struts Label Logic:iterate usage details

Source: Internet
Author: User
Tags documentation tld
Core tip: Iterate is mainly used to process the output collection class on the page, the collection is generally one of the following: 1, the Java Object Array 2, ArrayList, Vector, HashMap and other specific usage please refer to the Struts documentation, not detailed introduction Now define a Class,user.java to compile it into the User.class package example; Import java.io iterate is primarily used to process output collection classes on a page, and the collection is generally one of the following:
1. An array of Java objects

2, ArrayList, Vector, HashMap, etc.


Please refer to the Struts documentation for specific usage, no details are provided here.

Now define a Class,user.java to compile it into User.class

package example;        import java.io.serializable;     Public  final class User implements Serializable {     private string  name = null;     private string password = null;         public string getname  ()  {     return  (this.name);     }        public void setname (string name)  {     this.name = name;    }        public string  getPassword  ()  {     return  (this. password);    }        public void setpassword  (string password)  {     this. password = password;    }       }&nbsp   
Then create a JSP in a struts webapplication, such as iterate.jsp

<%@ page language= "Java"  %>     <%@ page import= "example.*"% >     <%@ taglib uri= "/web-inf/struts-bean.tld"  prefix= "Bean"  %>      <%@ taglib uri= "/web-inf/struts-logic.tld"  prefix= "logic"  %>        <%     java.util.arraylist list = new  Java.util.ArrayList ();     user usera=new user ();     usera.setname ("White ");     Usera.setpassword (" ABCD ");     list.add (UserA);     user  Userb=new user ();     userb.setname ("Mary");     Userb.setpassword ("Hijk");      List.add (UserB);     session.setattribute ("list",  list);       %>       

Put User.class, iterate.jsp into the appropriate directory, run iterate.jsp you can see the effect of iterate



Iterate Mark
The name of the ID script variable that holds the handle to the current element in the collection.
Name represents the set of iterations you need, from the properties of the session or request.
Type is one of the types of the collection class element

The write token of the bean is used to output the property, and the name used to match iterate's id,property is used to match the properties of the corresponding class <logic:iterate> usage 22007-04-04 20:34<login: The iterate> tag is used to create a loop in the page that iterates over objects such as arrays, Collection, and maps. The markup is powerful and is often used in struts application pages.
1. Loop through an array
Using the <logic:iterate> tag can be used to iterate over an array, here is a sample code:
Program code <% string[] testarray={"str1", "str2", "STR3"};     Pagecontext.setattribute ("Test", Testarray); %> <logic:iterate id= "show" name= "Test" > <bean:write name= "show"/> </logic:iterate>

In the preceding code, a string array is first defined and initialized for it. Next, the array is stored in the PageContext object, named Test1. It then uses the Name property of the <logic:iterate> tag to specify the array and uses the ID to refer to it, using the <bean:write> tag to display it. The result is: str1 str2 STR3

In addition, you can specify the number of output elements by using the Length property. As in the following code:
Program code <logic:iterate id= "show" name= "Test" length= "2" offset= "1" > <bean:write name= "Show"/> </logic: Iterate>

Where the Length property specifies the number of output elements, the offset property specifies that the output starts from the first few elements, so that it is 1, which means that the output starts at the second element. So the result of this code should be output: str2 STR3

Additionally, the tag has a IndexID property that specifies a variable that holds the ordinal number of the element being accessed in the current collection, such as:
Program code <logic:iterate id= "show" name= "Test" length= "2" offset= "1" indexid= "number" > <bean:write name= "number"/& Gt;:<bean:write name= "Show"/> </logic:iterate>

It displays the result: 1:str2 2:STR3



2. Loop through the HashMap
Program code <% HASHMAP countries=new HashMap ();    Countries.put ("Country1", "China");    Countries.put ("Country2", "United States");    Countries.put ("Country3", "United Kingdom");    Countries.put ("Country4", "France");    Countries.put ("Country5", "Germany");     Pagecontext.setattribute ("Countries", countries); %> <logic:iterate id= "Country" name= "countries" > <bean:write name= "Country" property= "key"/>: < Bean:write name= "Country" property= "value"/> </logic:iterate>

The

Gets the key and value of the Haspmap object, respectively, through the property's key and value in Bean:write. The result is: Country5: Germany      country3: UK      Country2: USA      COUNTRY4: France      country1: China    

The

is seen by the results, and it is not displayed in the order in which it was added. This is because haspmap are stored in disorder.

3, nested traversal
Program code <%    string[] colors={"Red", "green", "Blue"};     string[] countries1={"China", "USA", "France"};    string[] persons={"Jordan", "Bush", "Clinton"};    arraylist list2=new arraylist ();    list2.add (colors);    List2.add (countries1);    list2.add (persons);    Pagecontext.setattribute ("List2", List2);   %>   <logic:iterate id= "First"  name= "List2"  indexid= "Numberfirst" >   <bean:write name= "Numberfirst"/>   <logic:iterate id= "second"  name= "First" >   <bean:write name= "second"/>   </logic:iterate>    <br>   </logic:iterate>  

Run Effect: 0 red green blue     1  China   USA   France      2  Jordan & nbsp, Bush   Clinton     

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.