Struts1 label & lt; logic: iterate & gt;, struts1iterate

Source: Internet
Author: User
Tags tld

Struts1 label <logic: iterate>, struts1iterate
<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:

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

How to obtain the value of the hidden domain in the logic: iterate tag of struts1

<A href = "javascript: getQuesId (<bean: write name =" ques "property =" questionId "/>);"> <bean: write name = "ques" property = "questionContext"/> </a>
In this way, you can
Function getQuesId (id ){
// Upload the bar ID as a parameter to the function
Alert (id );

How to Use the logic: equal label in struts1?

It is mainly used for comparison. The usage of specific attributes is as follows:
<Logic: equal name = "RegistrationForm" property = "action" value = "Create">
Name = "Object name" property = "property name" value = "compare value"
You can use the c: if tag in jsp, which is a little more advanced than the if tag, but almost

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: 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.