The first type: struts1.2 loop through the elements, name is the result set. Quote tag library <%@ taglib uri="/tags-logic" prefix="logic" %>
<logic:iterate> is mainly used to process output collection classes on pages. Collections are generally one of the following:
1. Array of java objects
2. ArrayList, Vector, HashMap, etc.
example:
Copy the codeThe code is as follows:
<logic:iterate name="list" type=" ">
<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 tag
id The name of the script variable, which holds the handle of the current element in the collection.
name represents the collection you need to be substituted, from the session or request attribute.
type is the type of the collection class element in it
The write tag of bean is used to output the attribute, name is used to match the id of iterate, property is used to match the attributes of the corresponding class. Detailed explanation of the usage of <logic:iterate>22007-04-04 20:34<login:iterate> tag is used to create a loop in the page to traverse objects such as arrays, Collection, and Map. This tag is powerful and is often used in the Struts application pages.
1. Looping the array
Use the <logic:iterate> tag to be used to iterate through an array, and the following is a sample piece of code:
Program code
Copy the codeThe code is as follows:
<%
String[] testArray={"str1","str2","str3"};
("test",testArray);
%>
<logic:iterate name="test">
<bean:write name="show"/>
</logic:iterate>
In the above code, first, an array of strings is defined and initialized for it. Next, store the array into the pageContext object and name it test1. Then use the name attribute of the <logic:iterate> tag to specify the array, and use the id to reference it, and use the <bean:write> tag to display it. The result is:
str1
str2
str3
In addition, the number of output elements can also be specified through the length attribute. As shown in the following code:
Program code <logic:iterate 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. As shown in 1, it means that the output starting from the second element. So the running result of this code should output:
str2
str3
In addition, this tag has an indexId attribute, which specifies a variable to store the sequence number of the element being accessed in the current set, such as:
Program code
Copy the codeThe code is as follows:
<logic:iterate name="test" length="2" offset="1" indexId="number">
<bean:write name="number"/>:<bean:write name="show"/>
</logic:iterate>
The display result is:
1:str2
2:str3
2. Circular traversal of HashMap
Program code
Copy the 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",countries);
%>
<logic:iterate name="countries">
<bean:write name="country" property="key"/>:
<bean:write name="country" property="value"/>
</logic:iterate>
As can be seen from the results, it does not display it in the order it was added. This is because HaspMap is stored in an unorderly manner.
3. Nested traversal
Program code
Copy the codeThe code is as follows:
<span style="color:#CC6600;"><%
String[] colors={"red","green","blue"};
ArrayList list2=new ArrayList();
(colors);
(countries1);
(persons);
("list2",list2);
%>
<logic:iterate name="list2" indexId="numberfirst">
<bean:write name="numberfirst"/>
<logic:iterate name="first">
<bean:write name="second"/>
</logic:iterate>
<br>
</logic:iterate>
Running effect:
0 red green blue
The second type: struts2.0 loops through elements, note: value is the result set. Quote tag library <%@ taglib uri="/struts-tags" prefix="s" %>
The s:iterator tag has 3 properties:
value: the iterated collection
id: Specify the id of the element in the collection
status Index of iterative elements
1: jsp page definition element writing method array or list
Copy the codeThe code is as follows:
<span style="color:#CC6600;"><s:iterator value="{'1','2','3','4','5'}" id='number'>
<s:property value='number'/>A
</s:iterator>
Printing result is: 1A2A3A4A5A</span>
2: Usage of indexes
If status is specified, each iteration data has an instance of IteratorStatus. It has the following methods
int getCount() returns several elements currently iterated
int getIndex() returns the current element index
boolean isEven() of course, is the index even?
boolean isFirst() is the current first element
boolean isLast()
boolean isOdd() is the current element index odd number
Copy the codeThe code is as follows:
<span style="color:#CC6600;"><s:iterator value="{'a','b','c'}" id='char' status='st'>
<s:if test="#">
The index is now odd:<s:property value='#'/>
</s:if>
Current element value: <s:property value='char'/>
</s:iterator> </span>
3: traversal map
Value can be defined directly as:
value can also be an object in the data stack
Copy the codeThe code is as follows:
<span style="color:#CC6600;">The traversal is written as follows:
<s:iterator value="map" 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 the codeThe code is as follows:
<span style="color:#CC6600;"><s:iterator value="label" >
<s:property value="#" />
</s:iterator>
Of course, value can also be written as value="%{label}" label can have operation.
The property List of label can be written as value="%{}" which is equivalent to: getLabel().getList();</span>
5: traverse 2 lists;
Copy the codeThe code is as follows:
<span style="color:#CC6600;">List<AttrName> attrN {color,size,style}
List<AttrValue> attrV {red,20,gay}
The elements of these two lists are one by one, one attrN corresponds to one attrV
<s:iterator value="%{attrN }" status="status">
index is : <s:property value=''/>
attrName is : <s:property value='id'/> or <s:property value='%{id}'/>
attrName is : <s:property value='%{attrV[#]}'/>
</s:iterator></span>
The third type: servlet uses the <c:> tag to loop through, note: items are the result set. Quote tag library <%@ taglib prefix="c" uri="/WEB-INF/" %>
The function of the <c:forEach> tag is to iterate the output content inside the tag. It can either perform a fixed number of iteration outputs, or determine the number of iterations based on the number of objects in the set.
The syntax definition of the <c:forEach> tag 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 the iteration parameter. The names of variables that can be used in the iterative body are used to represent each iterative variable. Type is String.
l items: The collection to be iterated. The types it supports will be explained below.
l varStatus: The name of the iteration variable, used to represent the state of the iteration, and you can access the information of the iteration itself.
l begin: If items are specified, then iteration starts from items[begin]; if items are not specified, then iteration starts from begin. Its type is an integer.
l end: If items are specified, then the iteration ends at items[end]; if items are not specified, then the iteration ends at end. Its type is also an integer.
l step: the step size of iteration.
The items attribute of the <c:forEach> tag supports all standard collection types provided by the Java platform. Additionally, you can use this operation to iterate over elements in an array, including arrays of primitive types. The collection types it supports and the iterated elements are as follows:
l: The element obtained by calling iterator().
l: By the obtained example.
l: Iterator element.
l: Enumerate elements.
l Object instance array: array element.
l Basic type value array: wrapper array element.
l String delimited by commas: a divided substring.
l: The row obtained by SQL query.
Whether it is iterating over integers or sets, the varStatus property of <c:forEach> plays the same role. Like the var attribute, varStatus is used to create variables that define scope (changes only work within the current tag body). However, variables named by the varStatus property do not store the current index value or the current element, but rather give an instance of the class. This class contains a series of features that describe the current state of the iteration, as the following attributes have the following meanings:
l current: The item (in the collection) of the current iteration.
l index: The iteration index starting from 0 in the current iteration.
l count: The count of iterations that start from 1 in the current iteration.
l first: used to indicate whether the current iteration is the first iteration. This property is of type boolean.
l last: used to indicate whether the current iteration is the last iteration. This property is of type boolean.
l begin: the value of the begin property.
l end: the value of the end attribute
l step: the value of step attribute
<c:foreach> Usage
It can be used as your study notes
<c:foreach> Similar to for and foreach loops Here are the usages I've seen so far:
1. Loop traversal and output all elements.
<c:foreach items="${list}" var="li">
${li}
</c:foreach>
Note: items are used to receive collection objects, and var defines objects to receive each element traversed from the collection. At the same time, it will automatically transform.
2. Loop traversal and output elements of a range class.
<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. Quotations for begin and end must be written.
3. Loop traversal, output elements other than one element or output specified elements.
<c:foreach items="${list}" var ="li" varStatus="status">
<c:if text="${==1}>
${"No first element"}
</c:if>
${li}
</ c:foreach>
Note: varStatus represents the current status of the collection (actually, I don’t know if it is, I only know how to use it, and I will give you some advice). Count is a calculator for looping.
4. Loop traversal and output the first or last element.
<c:foreach items="${list}" var ="li" varStatus="status">
<c:if text="${}">I am the first element</c:if>
<c:if text="${}">I am the last element</c:if>
</c:foreach>
Note: first means that if it is an element, it returns ture, otherwise it returns false
last means that if it is the last element, it returns ture, otherwise it returns false.
5. Loop traversal and output according to the specified step size.
<c:foreach items="list" var ="li" step="2">
${li}
</c:foreach>
Note: step is the step size of the loop. Each time, one is outputted in two units. For example: 1, 3, 5, ==