Dark Horse programmer [iterative tag library of JSTL], dark horse jstl

Source: Internet
Author: User

Dark Horse programmer [iterative tag library of JSTL], dark horse jstl

Iterative tag library of JSTL

The full name of JSTL is the Java Server Pages Standard Tag Library, translated as the JSP Standard Tag Library, which contains a set of Standard tags that are frequently used when developing JSP Pages. These labels provide a way to develop complex JSP pages without embedding Java code.

There are five types of tag libraries in JSTL, one of which is an important core tag library. The core tag library is divided into a general tag library, a condition tag library, and an iterative tag library based on different functions, today we are talking about the iterative tag library.

During JSP development, we often need to perform iterative operations on the collection objects. For example, the List displays query results. In the past, java code was usually used to traverse collection objects, such as List and Iterator. Now, the iteration tag of JSTL can greatly simplify iteration operations.

<C: forEach> A tag has two syntax formats: one for traversing the members of a collection object, and the other for setting the number of times the statement is executed cyclically. We will list them one by one.

1. traverse the members of the Set object

<C: forEach var = "varName" items = "collectionName" varStatus = "varStatuwsName" begin = "beginIndex" end = "endIndex" step = "step"> display content </c: forEach>

The above is the basic syntax format of a forEach tag. The var attribute represents the current traversal member, and the items is the set object to be iterated, the varStatus attribute is used to store information about referenced members of var, such as indexes. begin indicates the start position. The default value is 0. end indicates the end, and step indicates the cycle step, the default value is 1. Now that we know the basic syntax and functions of each attribute, what is it like to bring it into the program? Next we will use a simple Demo to give you an intuitive presentation:

<% List <String> list = new ArrayList <String> (); list. add ("James"); list. add ("red"); list. add ("Xiaogang"); request. setAttribute ("list", list); %> <c: forEach var = "varName" items = "$ {list}" varStatus = "varStatusName" >$ {varName} </c: forEach>

In this way, a simple iteration effect is achieved. Generally, JSTL labels must be used with EL expressions to obtain and output data easily. As you can see, it is very easy to traverse the loop object directly, so we will continue to look at another syntax format, that is, the number of times the statement is executed.

2. Number of times the specified statement is executed:

<C: forEach var = "varName" varStatus = "varStatuwsName" begin = "beginIndex" end = "endIndex" step = "step"> display content </c: forEach>

Isn't it familiar? Its syntax is not very different from above. The difference is that format 2 does not traverse a collection object, but is based on the formulated begin attribute, the fixed number of times that the end attribute and the step attribute executes the subject content. This method is widely used in actual development. In many development projects, the list and report should be displayed. Below we will use the core code of a person-worker list to demonstrate its effect:

<Table style = "border: 1px solid blue; width: 400px;"> <tr style = "font-weight: bold; "> <td> name </td> <td> position </td> <td> age </td> </tr> <! -- Added a set on the servlet page --> <c: forEach var = "varName" items = "$ {list}" varStatus = "status"> <tr <c: if test = "$ {status. index % 2 = 0} "> style =" background-color: pink "</c: if> </tr> <td >$ {varName. name} </td> <td >$ {varName. job} </td> <td >$ {varName. age} </td> </tr> </c: forEach> </table>

In this way, there is a good color effect on the line:

 

Is it easy to use? If you use jquery or HTML code to implement this connection style with the database, it will be very troublesome to use the JSTL label to make the code easier, and the condition label can make the code more changed.

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.