c:each tag explained in detail

The syntax of the <c:forEach> tag is defined 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 attributes:

l var: The name of the iteration parameter. The names of variables that can be used in the iteration body to represent each iteration variable. Type is String.

l items: The collection to be iterated over. The types it supports will be explained below.

l varStatus: The name of the iteration variable, which is used to indicate the status of the iteration, and the information of the iteration itself can be accessed.

l begin: If items is specified, the iteration starts from items[begin]; if no items is specified, it starts from begin. Its type is integer.

l end: If items are specified, the iteration ends at items[end]; if no items are specified, the iteration ends at end. Its type is also integer.

l step: The step size of the 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 arrays, including arrays of primitive types. The collection types it supports and the elements to iterate over are as follows:

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

l Object instance array: array elements.

l Array of basic type values: packed array elements.

l String delimited by comma: substring after splitting.

l javax.servlet.jsp.jstl.sql.Result: The row obtained by the SQL query.

  The varStatus attribute of <c:forEach> has the same effect whether iterating over integers or over collections. Like the var attribute, varStatus is used to create scoped variables (changes only work within the current tag body). However, the variable named by the varStatus property 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 properties that describe the current state of the iteration. The meanings of these properties are as follows:

l current: The item (in the collection) for the current iteration.

l index: The iteration index of the current iteration starting from 0.

l count: The iteration count starting from 1 for the current iteration.

l first: It is used to indicate whether the current iteration is the first iteration. This attribute is of type boolean.

l last: It is used to indicate whether the current iteration is the last iteration. This attribute is of type boolean.

l begin: The value of the begin attribute.

l end: the value of the end attribute

l step: the value of the step attribute

Let's take a look at a basic example, the background color of the table interlace changes
<c:forEach var="item" items="${contents}" varStatus="status">
  <tr <c:if test="${status .count%2==0}">bgcolor="#CCCCFE"</c:if> align="left">
  xxx </tr>
</c:forEach>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326983432&siteId=291194637