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>