Logic: Detailed description of iterate usage

Source: Internet
Author: User
Tags tld

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.

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 attribute. Name is used to match the ID of iterate, and property is used to match the attribute 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 using the name attribute of the <logic: iterate> tag, and its ID is used to reference it. The <Bean: Write> tag is used to display it. The result is:

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:

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>
</Logic: iterate>
Running effect:
0 red green blue
1 China, USA, France
2 Jordan 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.