Basic usage of C:foreach in JSP __js

Source: Internet
Author: User
In the development of JSP, iterations are the operations that are often used. For example, the results of a query are displayed on a line-by-row basis. In earlier JSPs, scriptlets was often used to implement iterative output of iterator or enumeration objects. Now, the iterative label through Jstl can simplify the iterative operation to a large extent.

The JSTL supports two iteration labels, <c:forEach> and <c:forTokens> respectively. Here are the <c:forEach> tags.

The simple point is that the function of the,<c:foreach> tag is to iterate through the contents of the output tag. It can be either a fixed number of iterations of the output, or the number of objects in the collection to determine the number of iterations.

<c:forEach> tags, need to be used in conjunction with El expressions
The syntax definitions for < C:foreach > tags are shown below.

< C:foreach var= "Each variable name items=" The list to iterate varstatus= "the state of each object"

Begin= "Where does the loop start" end = "loop to where" step= "cycle Step" >

Loop the thing to output

</C:foreach >

The < C:foreach > tags have some of the following properties:

L var: the name of an iteration parameter. The name of a variable that can be used in an iterator to represent each iteration variable. Type is string.

L Items: A collection to iterate through. The types that it supports are explained below.

L Varstatus: The name of the iteration variable that represents the state of the iteration and can access the information of the iteration itself.

Begin: If items are specified, the iteration begins with Items[begin, and if no items are specified, the iteration begins from begin. Its type is an integer.

L End: If items are specified, the iteration is ended in items[ending, and if no items are specified, the iteration ends at end. Its type is also an integer.

L Step: the iteration length.

The Items property of the < C:foreach > tab supports all the standard collection types provided by the Java platform. In addition, you can use this action to iterate over an element in an algebraic group, including an array of base types. The type of collection it supports and the elements of the iteration are as follows:

L Java.util.Collection: The element to be obtained by calling iterator ().

L JAVA.UTIL.MAP: an example obtained through Java.util.Map.Entry.

L Java.util.Iterator: iterator element.

L Java.util.Enumeration: enumeration element.

An object instance array: An array element.

L An array of primitive type values: wrapped array elements.

L A comma-delimited string: a segmented substring.

L Javax.servlet.jsp.jstl.sql.Result:SQL the rows obtained by the query.

The same is done for integers or for iterating,< C:foreach > Varstatus Properties of the collection. As with the Var property, Varstatus is used to create scoped variables (the amount of change only works in the current label body). However, a variable named by the Varstatus property does not store the current index value or the current element, but rather gives an instance of the Javax.servlet.jsp.jstl.core.LoopTagStatus class. The class contains a series of attributes that describe the current state of the iteration, and the following properties have the following meanings:

L Current: The item in this iteration (in the collection).

L Index: the iteration index at the current iteration starting at 0.

L Count: The current iteration count, starting at 1.

L: Used to indicate whether the current iteration is the first iteration, which is a Boolean type.

L: Used to indicate whether the current iteration is the last iteration, which is a Boolean type.

L The value of the Begin:begin property.

L The value of the End:end property

L The value of the Step:step property

Here are two basic examples, the first example is to sequentially output the elements in the collection.
< C:foreach var= "item" items= "${contents}" varstatus= "status" >
$status. Count:${item}
</C:foreach >
The following example is a fixed number of iterations that are used to output squares of 1 to 9.
< C:foreach var= "x" begin= "1" end = "9" step= "1" >
${X*X}

</c:forEach>

Here's a list of examples that I've used in my project:

Page pagination:

<table>
<tr><th> name </th><th> description </th><th> picture preview </th></tr>
<c:foreach items= "${data}" var= "Item" >
<tr><td>${item.advertname}</td><td>${item.notes}</td><td></td></tr>
</c:forEach>
</table>
<ul>
<li><a href= '? Nowpage=${nowpage-1} ' >← prev </a></li>
<c:foreach varstatus= "I" begin= "1" end= "${sumpage}" >
<c:choose>
<c:when test= "${nowpage==i.count}" >
<li class= ' disabled ' >${i.count}</li>
</c:when>
<c:otherwise>
<li class= ' active ' ><a href= ' Nowpage=${i.count} ' >${i.count}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
<li><a href= '? Nowpage=${nowpage+1} ' > next page →</a></li>
</ul>

Home Show examples of pictures:

Iterative background pass over the list, src path to write absolute path, written relative path will be an error, sometimes absolute path with <c:ulr> to write

<c:foreach items= "${lists}" var= "Item" >

</c:forEach>

Get subscript, where size is the length of the list passed in the background, which cannot be written as end= "${list.size}".

              <c:foreach begin= "1" End= "${size}"   step= "1" varstatus= "I"
                     <li> <a href= "http://www.baidu.com/" class= " Showimg ">${I.INDEX}</A></LI>
                  </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.