JSP 标准标签库(JSP Standard Tag Library,JSTL)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断、数据管理格式化、XML 操作以及数据库访问。——百度。
如果要使用JSTL,则需要将jstl.jar和 standard.jar文件导入到项目中。并且在JSP页面中添加相应的伪指令,例如:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.bjpowernode.com/myfunctions" prefix="my" %>
常用的JSTL标签有:
<c:out>
<c:if>
<c:choose>、<c:when>和<c:otherwise>
<c:forEach>
<fmt:formatNumber>
<fmr:formatDate>
函数库
实例:
<c:out>:
<h1>测试JSTL核心库</h1>
<hr>
<li>采用c:out标签</li><br>
hello(使用标签):<c:out value="123"/><br>
hello(使用标签):<c:out value="hello"/><br>
hello(使用标签):<c:out value="${hello}"/><br>
hello(使用EL表达式):${hello }<br>
hello(default):${hello123 }<br>
hello(使用缺省值):<c:out value="${hello123}" default="没有值"/><br>
hello(使用缺省值):<c:out value="${hello123}">没有值</c:out><br>
welcome(使用EL表达式):${welcome }<br>
welcome(使用标签,escapeXml=true):<c:out value="${welcome}" escapeXml="true"/><br>
welcome(使用标签,escapeXml=false):<c:out value="${welcome}" escapeXml="false"/><br>
<p>
<c:if>:
<li>条件控制标签:c:if</li><br>
<c:if test="${v1 lt v2}">
v1小于v2<br>
</c:if>
<c:choose>、<c:when>和<c:otherwise>:
<li>条件控制标签:c:choose,c:when,c:otherwise</li><br>
<c:choose>
<c:when test="${v1 gt v2}">
v1大于v2<br>
</c:when>
<c:otherwise>
v1小于v2<br>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${empty userList}">
没有符合条件的数据<br>
</c:when>
<c:otherwise>
存在用户数据<br>
</c:otherwise>
</c:choose>
<c:forEach>:
<h3>采用forEach标签</h3>
<table border="1">
<tr>
<td>用户名称</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty users}">
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.username }</td>
<td>${user.age }</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
…………
现在的软件开发注重分层设计,JSTL标签库的使用是为了规范自定义标签的使用和不希望JSP页面中出现java逻辑代码而诞生的。