<logic: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);
}
<%
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);
%>
<logic:iterate id= "A" name= "list" type= "example. User ">
<TR><TD width= "50%" >
Name: <bean:write name= "A" property= "name"/>
<TD/><TD width= "50%" >
Password: <bean:write name= "a" property= "password"/>
</td></tr>
</logic:iterate>
</table></body>
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"/>:<bean:write name= "show"/>
</logic:iterate>
It shows the result as:
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>
In Bean:write, the key and value of the Haspmap object are obtained by the property's key and value respectively. It shows the result as:
COUNTRY5: Germany
COUNTRY3: United Kingdom
Country2: USA
COUNTRY4: France
Country1: China
As can be seen from the results, 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", "United States", "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>
Operating effect:
0 Red Green Blue
1 China USA France
2 Jordambusklinton
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