Type 1: struts1.2 looping element. name indicates the result set. Reference tag library <% @ taglib uri = "http://struts.apache.org/tags-logic" prefix = "logic" %>
<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
Example:
Copy codeThe Code is as follows:
<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>
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
Copy codeThe Code is as follows:
<%
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
Copy codeThe Code is as follows:
<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
Copy codeThe Code is as follows:
<Span style = "color: # FF6600;"> <span style = "background-color: rgb (255,153,102);"> <span style = "background-color: rgb (255,255,255); "> <%
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 </span>
The result shows that it is not displayed in the order of addition. This is because HaspMap is unordered.
3. nested Traversal
Program code
Copy codeThe Code is as follows:
<Span style = "color: # CC6600;"> <%
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 [/size] [/size] [/color] [/color] </span>
Type 2: struts2.0 looping element. Note: value is the result set. Reference the tag library <% @ taglib uri = "/struts-tags" prefix = "s" %>
The s: iterator tag has three attributes:
Value: the set to be iterated.
Id: id of the element in the Set
Index of the status iteration Element
1: jsp page definition element writing array or list
Copy codeThe Code is as follows:
<Span style = "color: # CC6600;"> <s: iterator value = "{'1', '2', '3', '4 ', '5'} "id = 'number'>
<S: property value = 'number'/>
</S: iterator>
The result is 1A2A3A4A5A. </span>
2: Index usage
If status is specified, each iteration of data has an IteratorStatus instance, which has the following methods:
Int getCount () returns several elements of the current iteration.
Int getIndex () returns the index of the current element.
Boolean isEven (): Of course, whether the index is even
Boolean isFirst () indicates whether the current element is the first element.
Boolean isLast ()
Boolean isOdd () whether the index of the current element is odd
Copy codeThe Code is as follows:
<Span style = "color: # CC6600;"> <s: iterator value = "{'A', 'B ', 'C'} "id = 'Char 'status = 'st'>
<S: if test = "# st. Even">
The current index is odd: <s: property value = '# st. Index'/>
</S: if>
Current element value: <s: property value = 'Char '/>
</S: iterator> </span>
3: traverse map
Value can be directly defined:
Value can also be a java. util. Map object in the data stack.
Copy codeThe Code is as follows:
<Span style = "color: # CC6600;"> the Traversal method is as follows:
<S: iterator value = "map" id = "id" status = "st">
Key: <s: property value = 'key'/>
Value: <s: property vlaue = 'value'/>
</S: iterator>
Of course, both key and value can make java Object </span>
4: traverse the data stack. Simple List class,
List <Attr> class Attr {String attrName; String getAttrName () {return "123 ";}}
Copy codeThe Code is as follows:
<Span style = "color: # CC6600;"> <s: iterator value = "label" id = "id">
<S: property value = "# id. attrName"/>
</S: iterator>
Of course, value can also be written as value = "% {label}" label and can have. Operation
The attribute List of the label can be written as value = "% {label. list}", which is equivalent to: getLabel (). getList (); </span>
5: traverse two lists;
Copy codeThe Code is as follows:
<Span style = "color: # CC6600;"> List <AttrName> attrN {color, size, style}
List <AttrValue> attrV {red, 20, gay}
The two list elements correspond one by one, and one attrN corresponds to one attrV.
<S: iterator value = "% {attrN}" id = "id" status = "status">
Index is: <s: property value = 'status. Index'/>
AttrName is: <s: property value = 'id'/> or <s: property value = '% {id}'/>
AttrName is: <s: property value = '% {attrV [# status. index]}'/>
</S: iterator> </span>
Third: serlvet uses the <c:> tag loop traversal. Note: items is the result set. Reference tag library <% @ taglib prefix = "c" uri = "/WEB-INF/c. tld" %>
<C: forEach> A tag is used to iterate the content inside the tag. It can be a fixed number of iterations, or the number of iterations can be determined based on the number of objects in the set.
<C: forEach> the syntax of a tag is defined as follows.
<C: forEach var = "name" items = "expression" varStatus = "name"
Begin = "expression" end = "expression" step = "expression">
Body content
</C: forEach>
<C: forEach> A tag has the following attributes:
L var: name of the iteration parameter. The name of the variable that can be used in the iteration body to represent each iteration variable. The type is String.
L items: the set to be iterated. The supported types are described below.
L varStatus: the name of the iteration variable, used to indicate the iteration status, and can access the information of the iteration itself.
L begin: If items is specified, iteration starts from items [begin]. If items is not specified, iteration starts from begin. Its type is an integer.
L end: If items is specified, the iteration ends at items [end]. If items is not specified, the iteration ends at end. Its type is also an integer.
L step: the iteration step.
<C: forEach> the items attribute of the tag supports all standard set types provided by the Java platform. In addition, you can use this operation to iterate elements in an array (including an array of basic types. It supports the following Collection types and iteration elements:
L java. util. Collection: Elements obtained by calling iterator.
L java. util. Map: the Instance obtained through java. util. Map. Entry.
L java. util. Iterator: Iterator element.
L java. util. Enumeration: Enumeration element.
L Object instance array: array element.
L basic type value array: packaged array elements.
L String to be bounded by commas (,): The Sub-String after segmentation.
L javax. servlet. jsp. jstl. SQL. Result: The row obtained by the SQL query.
Whether it is an integer or an iteration of a set, the varStatus attribute of <c: forEach> plays the same role. Like the var attribute, varStatus is used to create a variable with a limited scope (the change volume only works in the current TAG body ). However, the variable named by the varStatus attribute does not store the current index value or the current element, but is assigned to an instance of the javax. servlet. jsp. jstl. core. LoopTagStatus class. This class contains a series of features that describe the current state of iteration. The meanings of these attributes are as follows:
L current: the items in the current iteration (in the set.
L index: the index of the current iteration that starts from 0.
L count: the number of iterations of the current iteration starting from 1.
L first: indicates whether the current iteration is the first iteration. This attribute is of the boolean type.
L last: indicates whether the current iteration is the last iteration. This attribute is of the boolean type.
L begin: the value of the begin attribute.
L end: end Attribute Value
L step: the value of the step attribute
<C: foreach> usage
It can be used as your own learning notes.
<C: foreach> similar to for and foreach loops:
1. cyclically traverse and output all elements.
<C: foreach items = "$ {list}" var = "li">
$ {Li} www.jb51.net
</C: foreach>
Note: items is used to receive collection objects. var defines an object to receive each element traversed from the set. At the same time, it will automatically transform.
2. cyclically traverse the output of a range element.
<C: foreach items = "$ {lis}" var = "li" begin = "2" end = "12">
$ {Li}
</C: foreach>
Note: begin defines the start position of the traversal, and end defines the end position of the traversal. Quotation marks of begin and end must be written.
3. Loop traversal: outputs elements other than a specific element or outputs a specified element.
<C: foreach items = "$ {list}" var = "li" varStatus = "status">
<C: if text = "$ {status. count = 1}>
$ {"The first element is not "}
</C: if>
$ {Li}
</C: foreach>
Note: varStatus indicates the status of the current set (in fact, I am not very clear about it, only know how to use it, will be directed), count is a cycle calculator.
4. Loop traversal: outputs the first or last element.
<C: foreach items = "$ {list}" var = "li" varStatus = "status">
<C: if text = "$ {status. first}"> I am the first element </c: if>
<C: if text = "$ {status. last}"> I am the last element </c: if>
</C: foreach>
Note: first indicates that if it is an element, true is returned, and false is returned.
Last indicates that if the last element is used, true is returned. Otherwise, false is returned.
5. cyclically traverse and output according to the specified step size.
<C: foreach items = "list" var = "li" step = "2">
$ {Li}
</C: foreach>
Note: step is the cycle step. Each time, two units output one. Such as: 1, 3, 5, =