[Jstl] label usage

Source: Internet
Author: User

In JSP In development, iteration is often used. For example, the query results are displayed row by row. In the early days JSP , Usually Scriptlets To achieve Iterator Or Enumeration Iterative output of objects. Now Jstl The iteration tag can greatly simplify the iteration operation.

jstl two supported iteration labels are available: and . label.

To put it simply,<C: foreach>The role of a tag is to iteratively output 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>The syntax of a tag is defined as follows.

<C: foreach Var = "name" items = "expression" varstatus = "name"

Begin = "expression" End = "expression" step = "expression">

Body content

</C: foreach>

<C: foreach>Tags have the following attributes:

L 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. Type: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 access the information of the iteration itself.

L Begin : If Items So the iteration starts from Items [begin] Start iteration; if not specified Items From Begin Start iteration. Its type is an integer.

L End : If Items In Items [end] End iteration; if not specified Items In End End iteration. Its type is also an integer.

L step : iteration step.

tag items attribute support JAVA all standard set types provided by the platform. In addition, you can use this operation to iterate elements in an array (including an array of basic types. The supported collection types and iteration elements are as follows:

L JAVA. util. collection : call iterator () .

L Java. util. Map: PassJava. util. Map. EntryThe obtained instance.

L Java. util. iterator: Iterator element.

L Java. util. Enumeration: Enumeration element.

L ObjectInstance array: array element.

L Basic Type value array: Elements of the encapsulated array.

L string : The substring after splitting.

L Javax. servlet. jsp. jstl. SQL. Result:SQLQuery the obtained rows.

Whether it's an integer or an iteration of a set, <C: foreach> Of Varstatus Attributes play the same role. And VaR Same attributes, Varstatus Used to create a variable with a limited scope (the change volume only works in the current TAG body ). However Varstatus The attribute name variable does not store the current index value or the current element, but is assigned 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 : current iteration from 0 iterative index at the beginning.

L count : current iteration from 1 Start iteration count.

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

L last : indicates whether the current iteration is the last iteration. The attribute is Boolean type.

L begin : begin attribute value.

L end : end attribute value

L Step:StepAttribute Value

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 used to output1To9.

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

$ {X * x}

</C: foreach>

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.