JSP knowledge 01 --

Source: Internet
Author: User
<C: foreach> A tag is used to iterate the content inside the tag. It can be a fixed number of iterations, or the number of iterations can be determined based on the number of objects in the set. <C: foreach Var = "name" items = "expression" varstatus = "name" begin = "expression" End = "expression" step = "expression"> body content </C: foreach>

<C: foreach> A tag has the following attributes:

L var: 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.

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

L varstatus: the name of the iteration variable, used to indicate the iteration status, and can access the information of the iteration itself.

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

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

L step: the iteration step.

<C: foreach> the items attribute of the tag 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:

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

L object instance array: array element.

L basic type value array: packaged array elements.

L string to be bounded by commas (,): The Sub-string after segmentation.

L 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 of <C: foreach> 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:

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

L index: the index of the current iteration that starts from 0.

L count: the number of iterations of the current iteration starting from 1.

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

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

L begin: the value of the begin attribute.

L end: End Attribute Value

L step: the value of the Step attribute

The following are two basic examples. The first example is to output elements in the Set in sequence.

<C: foreach Var = "item" items = "$ {contents}" varstatus = "status">

$ Status. Count: $ {item}

</C: foreach>

The following example is a fixed number of iterations, which is used to output a square value ranging from 1 to 9.

<C: foreach Var = "X" begin = "1"
End = "9" step = "1">

$ {X * x}

</C: foreach>

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.