Struts1 label (logic: iterate)
<Logic: iterate> this class is mainly used to process the output set classes on pages. A 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.
Now define a class, which is compiled into User. class by User. java.
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;
}
}
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 );
%>
& Lt; html & gt; & lt; body & gt; & lt; table width = "100%" & gt;
<Logic: iterate id = "a" name = "list" type = "example. User">
& Lt; tr & gt; & lt; td width = "50%" & gt;
Name: <bean: write name = "a" property = "name"/>
& Lt; td/& gt; & lt; td width = "50%" & gt;
Password: <bean: write name = "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 the property. name is used to match the iterate id, and property is used to match the property of the corresponding class. <logic: iterate> Usage Details 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
The <logic: iterate> flag can be used to traverse arrays. The following is an example 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 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 following code:
Program code <logic: iterate id = "show" name = "test" length = "2" offset = "1">
<Bean: write name = "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 the Code 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:
Program code <logic: iterate id = "show" name = "test" length = "2" offset = "1" indexId = "number">
<Bean: write name = "number"/>:< bean: write name = "show"/>
</Logic: iterate>
The result is as follows:
1: str2
2: str3
2. cyclically traverse HashMap
Program 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: 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 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 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>
Running effect:
0 red green blue
1 China, USA, France
2 Jordan Bush Clinton