About <C: foreach>

Source: Internet
Author: User

<C: foreach>
<C: foreach> is a loop control that allows you to browse the members of a collection in sequence. When the conditions are met, the <C: foreach> body content is repeatedly executed.

Syntax

Syntax 1: iterate all the members of a set object

<C: foreach [Var = "varname"] items = "Collection" [varstatus = "varstatusname"]

[Begin = "begin"] [END = "end"] [step = "Step"]>

Body content

<

/C: foreach>

Syntax 2: number of iterations

<C: foreach [Var = "varname"] [varstatus = "varstatusname"] Begin = "begin" End = "end" [step = "Step"]>

Body content

</C: foreach>

Tags have the following attributes:

VaR: the 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.

Items: the set to be iterated. The supported types are described below.

Varstatus: the name of the iteration variable. It is used to indicate the iteration status and can access the information of the iteration itself.

Begin: If items is specified, iteration starts from items [begin]. If items is not specified, iteration starts from begin. Its type is an integer.

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.

Step: the iteration step.

The items attribute of tags 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:

Java. util. Collection: Elements obtained by calling iterator.

Java. util. MAP: the Instance obtained through java. util. Map. Entry.

Java. util. iterator: iterator element.

Java. util. enumeration: enumeration element.

Object instance array: array element.

Basic Type value array: Elements of the encapsulated array.

String to be separated by commas (,): The Sub-string after segmentation.

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 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:

Current: the items in the current iteration (in the set.

Index: The index of the current iteration that starts from 0.

Count: the number of iterations of the current iteration starting from 1.

First: indicates whether the current iteration is the first iteration. This attribute is of the boolean type.

Last: indicates whether the current iteration is the last iteration. This attribute is of the boolean type.

Begin: the value of the begin attribute.

End: End Attribute Value

Step: the value of the Step attribute.

The following is a basic example: the background color of the table is changed.
XML Code
<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>
Restrictions

· If the begin attribute exists, the begin must be greater than or equal to 0.

· If the end attribute exists, it must be greater than begin

· If the step attribute exists, the step must be greater than or equal to 0.

Null and error handling

· If items is null, it is an empty set object.

· If begin is greater than or equal to items, iteration is not performed.

Description

If you want to browse a collection object sequentially and display its content, you must have the items attribute.

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.