Iterate is mainly used to process the output set class on the page. The set is generally one of the following:
1. array of Java objects
2. arraylist, vector, and hashmap
For details about the usage, refer to the struts document.
A class is being defined. User. Java compiles it into user. Class.
- Package example;
- Import java. Io. serializable;
- Publicfinalclass user implements serializable {
- Private string name = NULL;
- Private string Password = NULL;
- Public String getname (){
- Return (this. Name );
- }
- Publicvoid setname (string name ){
- This. Name = Name;
- }
- Public String GetPassword (){
- Return (this. Password );
- }
- Publicvoid setpassword (string password ){
- This. Password = password;
- }
- }
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 );
- %>
- <HTML> <body> <tablewidth = "100%">
- <Logic: iterateid = "A" name = "list" type = "example. User">
- & Lt; tr & gt; & lt; tdwidth = "50%" & gt;
- Name: <Bean: writename = "A" property = "name"/>
- <TD/> <tdwidth = "50%">
- Password: <Bean: writename = "A" property = "password"/>
- </TD> </tr>
- </Logic: iterate>
- </Table> </body>
Put user. Class, iterate. jsp to the corresponding directory. Run iterate. jsp and you will see the iterate effect.
Iterate flag
ID: the name of the script variable, which stores the handle of the current element in the set.
Name indicates the set to be stacked, from the attributes of the session or request.
Type is the type of the Collection class element.
The write tag of bean is used to output attributes. Name is used to match the iterate ID, and property is used to match the attributes of the corresponding class. <logic: iterate> usage explanation 22007-04-04
<Login: iterate> mark is used to create a loop on the page to traverse objects such as arrays, collections, and maps. This tag is powerful and is often used on Struts application pages.
1. cyclically traverse the Array
Use the <logic: iterate> flag to traverse the array. The following is a sample code: program code.
- <%
- String [] testarray = {"str1", "str2", "str3 "};
- Pagecontext. setattribute ("test", testarray );
- %>
- <Logic: iterateid = "show" name = "test">
- <Bean: writename = "show"/>
- </Logic: iterate>
In the code above, a string array is defined and initialized. Save the array to the pagecontext object and name it test1. Then, the array is specified by the name attribute marked with <logic: iterate>, and the array is referenced by ID. The <Bean: Write> flag is used to display the array. The result is:
- Str1
- Str2
- Str3
In addition, the Length attribute can be used to specify the number of output elements. The code below:
- <Logic: iterateid = "show" name = "test" length = "2" offset = "1">
- <Bean: writename = "show"/>
- </Logic: iterate>
The Length attribute specifies the number of output elements, and the offset attribute specifies the output starting from the first element. If this parameter is set to 1, the output starts from the second element. Therefore, the running result of this code generation should be output:
- Str2
- Str3
In addition, this tag also has an indexid attribute, which specifies a variable to store the sequence numbers of accessed elements in the current set, such as program code.
- <Logic: iterateid = "show" name = "test" length = "2" offset = "1" indexid = "Number">
- <Bean: writename = "Number"/>:< Bean: writename = "show"/>
- </Logic: iterate>
The result is as follows:
- 1: str2
- 2: str3
2. cyclically traverse hashmapProgram code
- <%
- Hashmap countries = new hashmap ();
- Countries. Put ("country1", "China ");
- Countries. Put ("country2", "USA ");
- Countries. Put ("country3", "UK ");
- Countries. Put ("country4", "France ");
- Countries. Put ("country5", "Germany ");
- Pagecontext. setattribute ("countries", countries );
- %>
- <Logic: iterateid = "country" name = "countries">
- <Bean: writename = "country" property = "key"/>:
- <Bean: writename = "country" property = "value"/>
- </Logic: iterate>
In Bean: Write, the key and value of the haspmap object are obtained through the key and value of the property respectively. The result is as follows:
- Country5: Germany
- Country3: UK
- Country2: USA
- Country4: France
- Country1: China
The result shows that it is not displayed in the order of addition. This is because haspmap is unordered.
3. nested TraversalSequential 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: iterateid = "first" name = "list2" indexid = "numberfirst">
- <Bean: writename = "numberfirst"/>
- <Logic: iterateid = "second" name = "first">
- <Bean: writename = "second"/>
- </Logic: iterate>
- <Br>
- </Logic: iterate>
Running effect:
- 0 red green blue
- 1. China, US, France
- 2 Jordan Bush Clinton