JSTL improves JSP to simplify the implementation of web pages (3)

Source: Internet
Author: User
Tags foreach
Process control
Let's turn to the process control and condition marking of JSTL. If you have used conditions and flow control statements in any language, theoretically there is nothing new here.
C: Test the if action processing simple condition statement. Calculates the value of a Boolean expression in the Test attribute. If the expression is true, the content of the body is calculated. In the following action, we also illustrate the var attribute of the standby option. For future use, the var property will save the test result on the page (if other scope attributes are not specified.
<C: if test = "$ {status. totalVisits = 1000000}" var = "visits">
You are the millionth visitor to our site! Congratulations!
</C: if>
The following shows the support for JSTL using c: choose, c: when, and c: otherwise exchange logic. A group of c: when actions may be included in an alternative tag. If any expression in the c: when block calculates the value as true, tests in the c: choose action are not required. If no test value in the c: when block is true, when the c: otherwise action content appears, the c: otherwise action content is calculated:
<C: choose>
<C: when test = "$ {item. type = book}">
...
</C: when>
<C: when test = "$ {item. type = electronics}">
...
</C: when>
<C: when test = "$ {item. type = toy}">
...
</C: when>
<C: otherwise>
...
</C: otherwise>
</C: choose>
C: The foreach action provides an easy way to iterate the elements of a set. If you want to iterate only a part of the set, you can use the begin, end, and step attributes to specify the start point, end point, and an incremental value. In the following example, we iterate the content of a set in the customerNames variable. In each loop, the next element is input into the variable name and calculated in the c: foreach action body:
<Table>
<C: forEach var = "name" items = "$ {customerNames}">
<Tr> <td> <c: out value = "$ {name}"/> </td> </tr>
</C: forEach>
</Table>
Do you remember the Java StringTokenizer class? With the c: forTokens action, you can use JSTL to obtain similar functions. This program fragment can be iterated through entries in the items String attribute using the delims attribute defined in the delims attribute. Note that the items attribute does not need to be a single character directly; it can be any valid EL expression:
<Table>
<C: forTokens items = "47,52, 53,55, 46,22, 16,2" delim = "," var = "dailyPrice">
<Tr> <td> <c: out value = "$ {dailyPrice}"/> </td> </tr>
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.