C-label forEach, label foreach

Source: Internet
Author: User

C-label forEach, label foreach
<C: forEach> A tag is used for general data loops. It has the following attributes:

Genus Description Required? Default Value
Items Cyclic Project No None
Begin Start Condition No 0
End End Condition No The last project in the collection.
Step Step Size No 1
Var Variable name of the current project No None
VarStatus Variable that shows the loop status No None

VarStatus attribute

The varStatus attribute in JSTL is the same as the varStatus attribute. varStatus is used to create a variable with a limited scope. 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 defines a set of features that describe the current state of iteration. These features are listed below:

Feature Getter description current getCurrent () index getIndex () index of the current iteration (in the Set) current iteration index from 0 count getCount () first isFirst () indicates whether the current iteration is the first iteration () indicates whether the current iteration is the sign of the last iteration. The begin getBegin () begin attribute value end getEnd () end attribute value step getStep () step Attribute Value

Use the vaStatus attribute in c: forEach to obtain the iteration status, for example:
<C: forEach var = "" items = "" varStatus = "status">
<C: out value = "$ {status. current}"/> current object
<C: out value = "$ {status. index}"/> index of this iteration
<C: out value = "$ {status. count}"/> number of iterations
<C: out value = "$ {status. first}"/> whether it is the first iteration object
<C: out value = "$ {status. last}"/> whether it is the last iteration object
</C: forEach>

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

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.

 

 

<%
Response. setCharacterEncoding ("GB2312 ");
ArrayList student = (ArrayList) request. getAttribute ("list ");
Int size = student. size ();
For (int I = 0; I <size; I ++)
{
Student stu = (Student) student. get (I );
Out. println ("SID =" + stu. getSid ());
Out. println ("SName =" + stu. getSname ());
Out. println ("Score =" + stu. getScore ());
}
%>

Rewrite the above code snippet with JSTL:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "GB2312" %>
<% @ Taglib uri ="
Http://java.sun.com/jsp/jstl/core"Prefix =" c "%>
<Html>

<Body>
<Table>
<C: forEach var = "stu" items = "$ {list}">
<Tr>
<Td>
SID =$ {stu. sid}
</Td>
<Td>
SName =$ {stu. sname}
</Td>
<Td>
Score =$ {stu. score}
</Td>
</Tr>
</C: forEach>
</Table>
</Body>
</Html>


<C: forEach> tag problems in jsp

<% @ Taglib prefix = "c" url = "java.sun.com/jsp/jstl/core" %>
With this, you cannot output <c: forEach items = "$ {userlist}" var = "user"> ''$ {userlist}" this is obtained from the servlet and passed to the user. Try this way.
 
How to Use the c label foreach in java?

Hello:
Var is the name of the iteration parameter, which can be used in the iteration body to represent each iteration variable.
Items is a set of iterations.
Because you put the user object into the request object, the key value "user" corresponds to the result set that should be filled in items.
The Code is as follows:
<C: forEach items = "$ {user}" var = "record" varStatus = "status">
<Tr>
<Td> <c: out value = "$ {record. a}"> </c: out> </td>
<Td> <c: out value = "$ {record. B}"> </c: out> </td>
</Tr>
</C: forEach>
<C: out> is also a jstl label for output.

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.