11.JSTL标准标签库
来源:华佗健康网
1.1 现有问题
1.2 什么是 JSTL?
JSTL:全称Java Server Pages Standard Tag Library
JSP标准标签库(JSTL)是一个JSP标签集合。
1.3 JSTL的作用
可对EL获取到的数据进行逻辑操作。
与EL合作完成数据的展示。
1.4 JSTL使用
- 导入两个 jar 文件:standard.jar 和 jstl.jar 文件拷贝到 /WEB-INF/lib/ 下
- 在JSP页面引入标签库<% @taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”>
1.5 核心标签
1.5.1 条件标签if判断
语法:<c:if test =“条件”> < /c:if>
<!-- test属性中是条件,但是条件需要使用EL表达式来书写 -->
<h3>条件标签:if</h3>
<c:if test="${8>2 }">
8大于2是成立的
</c:if>
<c:if test="${8<2 }">
8小于2是成立的
</c:if>
1.5.2 多条件choose判断
语法:<c:choose >
<c:when test=“条件1”>结果1< /c:when>
<c:when test=“条件2”>结果2< /c:when>
<c:when test=“条件3”>结果3< /c:when>
<c:otherwise >结果4< /c:otherwise>
< /c:choose>
<h3>条件标签:choose(等价于java中多重if)</h3>
<%-- 测试成绩等级 >90 优秀 >80 良好 >70 中等 >60及格--%>
<c:set var="score" value="80"></c:set>
<c:choose>
<c:when test="${score>=90 }">优秀</c:when>
<c:when test="${score>=80 }">良好</c:when>
<c:when test="${score>=70 }">中等</c:when>
<c:when test="${score>=60 }">及格</c:when>
<c:otherwise>不及格</c:otherwise>
</c:choose>
1.5.3 迭代foreach标签
语法:
<c:foreach var=“变量名” items=“集合” < /c:foreach>
<h3>测试list集合遍历获取学生列表</h3>
<table border="1" width="80%" bordercolor="red" cellspacing="0"
align="center">
<tr>
<th>学号</th>
<th>姓名</th>
<th>成绩</th>
<th>班级</th>
</tr>
<!-- var :遍历出的每一项使用变量先存储
items:集合(使用El表达式)
-->
<c:forEach items="${students}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.score}</td>
<td>${stu.classes}</td>
</tr>
</c:forEach>
</table>
总结:JSTL可以对代码进行流程控制,可以对获取的数据进行逻辑操作
因篇幅问题不能全部显示,请点此查看更多更全内容