流程控制标签主要有:<c:if>、<c:choose>、<c:when>、<c:otherwise>
1:<c:if>标签可以构造简单的“if-then”结构的条件表达式,如果条件表达式的结果为真就执行标签体部分的内容。
<body>
<form action="jstlTest1.jsp" method="post"><!--提交登陆信息到本页-->
<input type="text" name="username" value="${param.username }">
<input type="submit" value="登陆">
</form>
<c:if test="${param.username!=null }" var="name">
<c:out value="欢迎您!"></c:out>
</c:if>
${param.username }
</body>
2:<c:choose>标签通常是与<c:when>、<c:otherwise>标签一起使用的。可以构造“if-else if -else”的复杂条件判断语句
<c:set value="${param.count }" var="count"></c:set>
<c:choose>
<c:when test="${count==0 }">
对不起,没有符合您要求的记录。
</c:when>
<c:otherwise>
符合您要求的记录共有${count }条。
</c:otherwise>
</c:choose>
在地址栏中输入:http://localhost/jstl/jstlTest1.jsp?count=1,输出结果为:符合您要求的记录共有1条。
循环标签有:<c:forEach>、<c:forTokens>
1:<c:forEach>标签用于对一个集合对象中的元素进行循环迭代操作,或者按指定的次数重复迭代执行标签体中的内容。
迭代Collection类型的对象
<%
Collection users = new ArrayList();
for(int i=0;i<3;i++){
UserBean user = new UserBean();
user.setUsername("user"+i);
user.setPassword("psd"+i);
users.add(user);
}
session.setAttribute("users", users);
%>
<table border="1">
<tr><td>用户名</td><td>密码</td></tr>
<c:forEach items="${users}" var="user">
<tr><td>${user.username }</td><td>${user.password }</td></tr>
</c:forEach>
</table>
迭代Map对象
<%
request.setAttribute("attr1","value1");
request.setAttribute("attr2","value2");
%>
<div style="text-align:center">Properties(Map)
<table border="1">
<tr><td>Map的关键字</td><td>Map的对应关键字的值</td></tr>
<c:forEach var="entry" items="${requestScope}">
<tr><td>${entry.key}</td><td>${entry.value}</td></tr>
</c:forEach>
</table></div>
迭代指定的次数
迭代步长为2:<c:forEach var="i" begin="0" end="5" step="2">${i }</c:forEach><br>
无迭代步长:<c:forEach var="i" begin="0" end="5">${i }</c:forEach>
获取迭代的状态信息
<table border="1">
<tr>
<td>用户名</td>
<td>密码</td>
<td>索引</td>
<td>当前已迭代的次数</td>
</tr>
<c:forEach items="${users}" var="user" varStatus="sta">
<tr>
<td>${user.username }</td>
<td>${user.password }</td>
<td>${sta.index }</td>
<td>${sta.count }</td>
</tr>
</c:forEach>
</table>
与条件标签结合使用,实现奇偶行变色:
<table border="1">
<c:forEach var="i" begin="4" end="9" varStatus="status">
<c:choose>
<c:when test="${status.count%2==0 }">
<tr bgcolor="pink">
</c:when>
<c:otherwise><tr></c:otherwise>
</c:choose>
<td>第${status.count }行</td>
<td>${i }</td>
<td><c:choose>
<c:when test="${status.count%2==0 }">偶数行</c:when>
<c:otherwise>奇数行</c:otherwise>
</c:choose></td>
</tr>
</c:forEach>
</table>
2:<c:forTokens>遍历含有符号的字符串,将指定的字符串移除。
<c:forTokens items="123-4567-8854" delims="-" var="num">
<c:out value="${num}©" escapeXml="false"></c:out>
</c:forTokens><br>
<c:forTokens items="123,45|67,|88|54" delims="," var="num">
<c:out value="${num}"></c:out>
</c:forTokens><br>
c:forTokens items="123,45|,67,|88|54" delims=",|" var="num">
<c:out value="${num}"></c:out>
</c:forTokens><br>
处理结果为123© 4567© 8854©
123 45|67 |88|54
123 45 67 88 54