JSP page Loop collection Operation _jsp programming

Source: Internet
Author: User

First: struts1.2 loops through the elements, name is the result set. Reference Tag Library <%@ taglib uri= "http://struts.apache.org/tags-logic" prefix= "logic"%>
<logic:iterate> is primarily used to process output collection classes on the page, which is generally one of the following:
1. An array of Java objects
2, ArrayList, Vector, HashMap, etc.
Cases:

Copy Code code as follows:

<logic:iterate id= "A" name= "list" type= "example. User ">
<TR><TD width= "50%" >
Name: <bean:write name= "A" property= "name"/>
<TD/><TD width= "50%" >
Password: <bean:write name= "a" property= "password"/>
</td></tr>
</logic:iterate>

Iterate Mark
The name of the ID script variable that holds the handle of the current element in the collection.
Name represents the collection you need to iterate over, from the session or request attribute.
Type is the kind of collection class element in which
The Bean's write tag is used to output the property, the name used to match the iterate id,property to match the attributes <logic:iterate> usages of the corresponding class 22007-04-04 20:34<login: The iterate> tag is used to create a loop in the page that iterates through objects such as arrays, Collection, and maps. The tag is powerful and is often used in pages that are applied to struts.

1. Iterate through the array
Use the <logic:iterate> tag to traverse an array, and here's a sample code:
Program code
Copy Code code 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 above code, you first define an array of strings and initialize them. Next, the array is stored in the PageContext object, named Test1. The array is then specified using the <logic:iterate> tag's Name property, which is referenced using an ID, and is displayed using the <bean:write> tag. The results are:
Str1
str2
Str3
In addition, you can specify the number of output elements by using the Length property. As in the following code:
Program code <logic:iterate id= "show" name= "Test" length= "2" offset= "1" >
<bean:write name= "Show"/>
</logic:iterate>
Where the Length property specifies the number of output elements, the offset property specifies that the output starts with the first few elements, so that it is 1, which means that the output starts at the second element. So the running result of the code should be output:
str2
Str3
In addition, the tag has a IndexID property that specifies a variable that holds the ordinal number of the element being accessed in the current collection, such as:
Program code
Copy Code code 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 results displayed are:
1:str2
2:str3

2, to the HashMap cycle traversal
Program code
Copy Code code 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", "United States");
Countries.put ("Country3", "England");
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 property's key and values respectively. The results displayed are:
COUNTRY5: Germany
Country3: UK
Country2: The United States
COUNTRY4: France
Country1: China </span> </span></span>

As you can see from the result, it does not display it in the order in which it was added. This is because the haspmap is stored in disorder.

3, nested traversal
Program code
Copy Code code as follows:

<span style= "color: #CC6600;" ><%
string[] colors={"Red", "green", "Blue"};
String[] countries1={"China", "United States", "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= "A" name= "List2" indexid= "Numberfirst" >
<bean:write name= "Numberfirst"/>
<logic:iterate id= "Second" name= "a" >
<bean:write name= "Second"/>
</logic:iterate>
<br>
</logic:iterate>
Operation Effect:
0 Red Green Blue
1 China USA France
2 Jordambusklinton [/size][/size][/color][/color]</span>

Second: struts2.0 loops through the elements, note: value is the result set. Reference Tag Library <%@ taglib uri= "/struts-tags" prefix= "s"%>
The S:iterator label has 3 properties:
Value: A collection of iterations
ID: Specifies the ID of the element within the collection
Index of the status iteration element
1:jsp page definition element style array or list
Copy Code code as follows:

<span style= "color: #CC6600;" ><s:iterator value= ' {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '} ' id= ' number ' >
<s:property value= ' number '/>a
</s:iterator>
Print Result: 1a2a3a4a5a</span>

2: Usage of the index
If status is specified, each iteration data has an instance of Iteratorstatus, which has the following methods
int GetCount () returns several elements of the current iteration
int GetIndex () returns the current element index
Boolean IsEven () of course the index is even
Boolean Isfirst () whether the first element is currently
Boolean islast ()
Boolean isodd () whether the current element index is odd
Copy Code code as follows:

<span style= "color: #CC6600;" ><s:iterator value= ' {' A ', ' B ', ' C '} ' id= ' char ' status= ' st ' >
<s:if test= "#st. Even ">
The index is now an odd number: <s:property value= ' #st. Index '/>
</s:if>
Current element value: <s:property value= ' char '/>
</s:iterator> </span>

3: Traverse Map
Value can be defined directly as:
Value can also be the Java.util.Map object in the data stack.
Copy Code code as follows:

<span style= "color: #CC6600;" The > traversal 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, 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 Code code 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 can have. Operation
The label's property list can be written as value= "%{label.list}" equivalent to: Getlabel (). GetList ();</span>

5: Traverse 2 list;
Copy Code code as follows:

<span style= "color: #CC6600;" >List<AttrName> attrn {Color,size,style}
List<attrvalue> ATTRV {Red,20,gay}
These 2 list elements are one by one corresponding, one attrn corresponds to a 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 with <c:> tags loop, note: Items are result sets. Reference Tag Library <%@ taglib prefix= "C" uri= "/web-inf/c.tld"%>
The function of <c:forEach> label is to iterate the content inside the output tag. It can be either a fixed number of iterations of the output, or the number of objects in the collection to determine the number of iterations.
The syntax definition for the <c:forEach> label is as follows.
<c:foreach var= "name" items= "expression" varstatus= "name"
begin= "expression" end= "expression" step= "expression" >
Body Content
</c:forEach>
The <c:forEach> tag has some of the following properties:
L var: the name of an iteration parameter. The name of a variable that can be used in an iterator to represent each iteration variable. Type is string.
L Items: A collection to iterate through. The types that it supports are explained below.
L Varstatus: The name of the iteration variable that represents the state of the iteration and can access the information of the iteration itself.
Begin: If items are specified, the iteration begins with Items[begin, and if no items are specified, the iteration begins from begin. Its type is an integer.
L End: If items are specified, the iteration is ended in items[end], and if no items are specified, the iteration ends at end. Its type is also an integer.
L Step: the iteration length.
The <c:forEach> tab's Items property supports all the standard collection types provided by the Java platform. In addition, you can use this action to iterate over an element in an algebraic group, including an array of base types. The type of collection it supports and the elements of the iteration are as follows:
L Java.util.Collection: The element to be obtained by calling iterator ().
L JAVA.UTIL.MAP: an example obtained through Java.util.Map.Entry.
L Java.util.Iterator: iterator element.
L Java.util.Enumeration: enumeration element.
An object instance array: An array element.
L An array of primitive type values: wrapped array elements.
L A comma-delimited string: a segmented substring.
L Javax.servlet.jsp.jstl.sql.Result:SQL the rows obtained by the query.
The same is the effect of the Varstatus property that iterates over the,<c:foreach> of integers or collections. As with the Var property, Varstatus is used to create scoped variables (the amount of change only works in the current label body). However, a variable named by the Varstatus property does not store the current index value or the current element, but rather gives an instance of the Javax.servlet.jsp.jstl.core.LoopTagStatus class. The class contains a series of attributes that describe the current state of the iteration, and the following properties have the following meanings:
L Current: The item in this iteration (in the collection).
L Index: the iteration index at the current iteration starting at 0.
L Count: The current iteration count, starting at 1.
L: Used to indicate whether the current iteration is the first iteration, which is a Boolean type.
L: Used to indicate whether the current iteration is the last iteration, which is a Boolean type.
L The value of the Begin:begin property.
L The value of the End:end property
L The value of the Step:step property
<c:foreach> usage
Can be used as a learning note for oneself
<c:foreach> similar to for and foreach loops This is the usage I've seen so far:

1, loop traversal, output all the elements.
<c:foreach items= "${list}" var= "Li" >
${li} www.jb51.net
</c:foreach>
Note: Items are used to receive collection objects, and the var definition object receives each element that is traversed from the collection. At the same time it will automatically transform.

2, loop traversal, output a range of elements of the class.
<c:foreach items = "${lis}" var = "Li" begin= "2" end = ">"
${li}
</c:foreach>
Note: Begin defines the starting position of the traversal, and end defines the ending position of the traversal. The quotation marks for begin and end must be written.

3, loop traversal, output in addition to an element or output specified elements.
<c:foreach items= "${list}" var = "Li" varstatus= "status" >
<c:if text= "${status.count==1}>
${"first element not"}
</c:if>
${li}
</c:foreach>
Note: Varstatus represents the state of the current collection (in fact, I'm not sure, I'm not too clear, just know to use, the person pointing to it), count for loop a calculator.

4, loop traversal, output 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: The top indicates that if it is an element, it returns ture, and vice versa returns false
Last indicates that if it is the final element, it returns ture, or false.

5, loop traversal, according to the specified step output.
<c:foreach items= "list" var = "Li" step= "2" >
${li}
</c:foreach>
Note: Step is the length of the loop. Output one per two units at a time. such as: 1, 3, 5, = =

Related Article

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.