Struts label-logic: 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.
A class is being defined. User. Java compiles it into user. Class.

  1. Package example;
  2. Import java. Io. serializable;
  3. Publicfinalclass user implements serializable {
  4. Private string name = NULL;
  5. Private string Password = NULL;
  6. Public String getname (){
  7. Return (this. Name );
  8. }
  9. Publicvoid setname (string name ){
  10. This. Name = Name;
  11. }
  12. Public String GetPassword (){
  13. Return (this. Password );
  14. }
  15. Publicvoid setpassword (string password ){
  16. This. Password = password;
  17. }
  18. }

Create a JSP in a struts webapplication, such as iterate. jsp.

  1. <% @ Page Language = "Java" %>
  2. <% @ Page import = "example. *" %>
  3. <% @ Taglib uri = "/WEB-INF/struts-bean.tld" prefix = "Bean" %>
  4. <% @ Taglib uri = "/WEB-INF/struts-logic.tld" prefix = "logic" %>
  5. <%
  6. Java. util. arraylist list = new java. util. arraylist ();
  7. User usera = new user ();
  8. Usera. setname ("white ");
  9. Usera. setpassword ("ABCD ");
  10. List. Add (usera );
  11. User userb = new user ();
  12. Userb. setname ("Mary ");
  13. Userb. setpassword ("hijk ");
  14. List. Add (userb );
  15. Session. setattribute ("list", list );
  16. %>
  17. <HTML> <body> <tablewidth = "100%">
  18. <Logic: iterateid = "A" name = "list" type = "example. User">
  19. & Lt; tr & gt; & lt; tdwidth = "50%" & gt;
  20. Name: <Bean: writename = "A" property = "name"/>
  21. <TD/> <tdwidth = "50%">
  22. Password: <Bean: writename = "A" property = "password"/>
  23. </TD> </tr>
  24. </Logic: iterate>
  25. </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.

  1. <%
  2. String [] testarray = {"str1", "str2", "str3 "};
  3. Pagecontext. setattribute ("test", testarray );
  4. %>
  5. <Logic: iterateid = "show" name = "test">
  6. <Bean: writename = "show"/>
  7. </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:

  1. Str1
  2. Str2
  3. Str3

In addition, the Length attribute can be used to specify the number of output elements. The code below:

  1. <Logic: iterateid = "show" name = "test" length = "2" offset = "1">
  2. <Bean: writename = "show"/>
  3. </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:

  1. Str2
  2. 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.

  1. <Logic: iterateid = "show" name = "test" length = "2" offset = "1" indexid = "Number">
  2. <Bean: writename = "Number"/>:< Bean: writename = "show"/>
  3. </Logic: iterate>

The result is as follows:

  1. 1: str2
  2. 2: str3

2. cyclically traverse hashmapProgram code

  1. <%
  2. Hashmap countries = new hashmap ();
  3. Countries. Put ("country1", "China ");
  4. Countries. Put ("country2", "USA ");
  5. Countries. Put ("country3", "UK ");
  6. Countries. Put ("country4", "France ");
  7. Countries. Put ("country5", "Germany ");
  8. Pagecontext. setattribute ("countries", countries );
  9. %>
  10. <Logic: iterateid = "country" name = "countries">
  11. <Bean: writename = "country" property = "key"/>:
  12. <Bean: writename = "country" property = "value"/>
  13. </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:

  1. Country5: Germany
  2. Country3: UK
  3. Country2: USA
  4. Country4: France
  5. 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

  1. <%
  2. String [] colors = {"red", "green", "blue "};
  3. String [] countries1 = {"China", "USA", "France "};
  4. String [] persons = {"Jordan", "Bush", "Clinton "};
  5. Arraylist list2 = new arraylist ();
  6. List2.add (colors );
  7. List2.add (countries1 );
  8. List2.add (persons );
  9. Pagecontext. setattribute ("list2", list2 );
  10. %>
  11. <Logic: iterateid = "first" name = "list2" indexid = "numberfirst">
  12. <Bean: writename = "numberfirst"/>
  13. <Logic: iterateid = "second" name = "first">
  14. <Bean: writename = "second"/>
  15. </Logic: iterate>
  16. <Br>
  17. </Logic: iterate>

Running effect:

  1. 0 red green blue
  2. 1. China, US, France
  3. 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.