This article transferred from: http://www.blogjava.net/caizh2009/articles/278999.html
In the development of JSP, iteration is the most frequently used operation. For example, a row-by-line display of the results of a query. In earlier JSPs, scriptlets was typically used to implement an iterative output of iterator or enumeration objects. Iterative tagging with JSTL can now simplify iterations to a large extent.
The JSTL supports two iteration tags, respectively <C:Foreach> and <C:fortokens>. The < is introduced here.C:foreach> tags.
Simple,,<.C:The purpose of the foreach> tag is to iterate over the contents of the output tag. It can either perform a fixed number of iterations, or determine the number of iterations depending on the number of objects in the collection.
<C:The syntax definition for the foreach> tag is as follows.
<C:ForEach var= "name" items= "expression" varstatus= "name"
begin= "Expression"end= "expression" step= "expression" >
Body Content
</C:Foreach>
<C:The foreach> tag has some of the following properties:
L var: the name of the iteration parameter. The name of the variable that can be used in the iteration to represent each iteration variable. Type is string.
L Items: the collection to iterate. 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 information about the iteration itself.
L Begin: If items are specified, the iteration begins with Items[begin] and, if items are not specified, iterates from begin. Its type is an integer.
LEnd: If items are specified, then the items[end], if no items are specified, theEnd ends the iteration. Its type is also an integer.
L Step: The step size of the iteration.
<C:The Items property of the foreach> tag supports all the standard collection types provided by the Java platform. In addition, you can use this action to iterate over the elements in an algebraic group, including an array of primitive types. The type of collection it supports and the elements of the iteration are as follows:
L Java.util.Collection: Call iterator () to get the element.
L JAVA.UTIL.MAP: an example obtained through Java.util.Map.Entry.
L Java.util.Iterator: iterator element.
L Java.util.Enumeration: enumeration element.
L Object Instance array: array element.
An array of primitive type values: wrapped array elements.
L string delimited by commas: the segmented substring.
L Javax.servlet.jsp.jstl.sql.Result:SQL the rows obtained by the query.
Whether to iterate over integers or collections,<C:The Varstatus property of the foreach> is the same as the function. Like the Var property, Varstatus is used to create scoped variables that only work in the current tag body. However, a variable named by the Varstatus property does not store the current index value or the current element, but instead assigns 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, with the following meanings:
L Current: The item (in the collection) for this iteration.
L Index: The iteration index of the current iteration starting at 0.
L Count: The iteration count that is currently starting at 1 for this iteration.
L First: is used to indicate whether the current iteration is the initial iteration, which is a Boolean type.
L Last: Used to indicate whether the current iteration is the final iteration, which is a Boolean type.
The value of the L Begin:begin property.
LEnd:The value of the End property
L STEP:STEP The value of the property
Here's a look at two basic examples, the first of which is to output the elements in the set sequentially.
<c:forEach var= "item" items= "${contents}" varstatus= "status" >
$status. Count:${item}
</C:foreach>
The following example is a fixed number of iterations that are used to output 1 to 9 squared.
<c:forEach var= "x" begin= "1" end= "9" step= "1" >
${X*X}
</C:foreach>
C:foreach detailed