Logic: iterate usage tutorial

Source: Internet
Author: User

From: http://www.cnblogs.com/kakaku/articles/1312826.html

 

<Logic: iterate> the tag 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 <Logic: iterate> Tags can be used to traverse arrays. The following is an example.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 use <Logic: iterate> The Marked name attribute specifies the array and uses ID to reference it. The <Bean: Write> flag is used to display it. The result is:
Str1
Str2
Str3

In addition, the Length attribute can be used to specify the number of output elements. The following code:
<Logic: iterateId = "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:

< 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

<%
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

<%
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
<Logic: iterate id = "it" name = "list" length = "2">
<Bean write name = "it"/> <br/>
</Logic: iterate>

List is the object you want to repeat. It is the type of the elements in the list, and there is an offset attribute, which is used to indicate the starting position, for example, offset = "1" traverses the value from the second position. offset = "0" is the default value.

 

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.