JSTL的目標是爲了簡化JSP頁面而設計。對於頁面設計人員來說,使用腳本語言(JSP默認是Java)操作動態數據是比較困難的,而採用標籤和表達式語言則相對容易些,JSTL的使用爲頁面設計人員和程序開發人員的分工協作提供了便利。
JSTL雖然叫做標準標籤庫,但實際上是由5個不同功能的標籤庫組成。這裏只介紹常用的Core標籤庫
Core標籤庫主要包括了一般用途的標籤、條件標籤、迭代標籤和URL相關的標籤。在JSP頁面中使用Core標籤庫,要使用taglib指令,指定引用的標籤庫,如下:
<%@ taglib uri="
http://java.sun.com/jsp/jstl/core
" prefix="c" %>
<s:set>
設置範圍變量的值
<s:set var="user" value="zhangsan" scope="session"/>
或
<s:set var="user" scope="session">zhangsan</s:set>
設置對象的屬性值
<s:set target="${user}" value="18" property="age"/>
或
<s:set target="${preferences}" property="color">${param.color}</c:set>
條件標籤:
例
<c:choose>
<c:when test="${param.name='zhangsan'}">
${param.name} is manager!
</c:when>
<c:when test="${param.name='lisi'}">
${param.name} is salesman!
</c:when>
<c:otherwise>
${param.name} is employee!
</c:otherwise>
</c:choose>
迭代標籤:
<c:forEach>用於對包含了多個對象的集合進行迭代
例
<table>
<c:forEach var="user" itmes="${useList}" varStatus="status">
<tr>
<td>${status.count}</td>
<td>${status.index}</td>
<td>${status.first}</td>
<td>${status.last}</td>
<td>${user.name}</td>
</tr>
</c:forEach>
</table>
開發中在JSP上最常用的也就這幾種,其他的基本是用不到...