C: forEach

Source: Internet
Author: User

In JSP development, iteration is often used. For example, the query results are displayed row by row. In early JSPs, Scriptlets is usually used to implement iterative output of Iterator or Enumeration objects. Now, the iteration tag of JSTL can greatly simplify iteration operations. JSTL supports two iteration labels: <c: forEach> and <c: forTokens>. The <c: forEach> label is described here. To put it simply, the <c: forEach> label is used 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> 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 iterative 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 the iteration. The meanings of these attributes are as follows: l current: The items of 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: the value of the end attribute l step: the value of the step attribute. Here are two basic examples. The first example is to output the 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 output the square of 1 to 9. <C: forEach var = "x" begin = "1" end = "9" step = "1" >$ {x * x} </c: forEach> instance supplement: <select id = "task_sort_id" style = "width: 120px; margin-left: 5px; height: 20px; font-size: 11px; "> <option value =" "selected =" selected "> <fmt: message key =" task. default "/> </option> <c: forEach items =" $ {sorts} "var =" sort "> <option value =" $ {sort. task_sort_id} ">$ {sort. task_sort_name} </option> </c: forEach> </select> sorts is a collection uploaded to the page. sort represents an instance in the collection.

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.