JSTL
Posted by Bruce Tsai
Core Tag Library
輸出、值的處理
<c:out/>
value: 輸出的內容。default: 當value為空白或空值時,輸出的預設值。escapeXml: 處理跳脫字元,預設為true。
<h2>Welcome, <c:out value="${user.name}" default="Guest" /></h2>
<c:set/>
value: 要賦與變數的值。target:property:var: 變數名稱。scope: 變數範圍。
<c:set var="dogAge" value="${age div 7}" />
<c:remove/>
var: 變數名稱。scope: 變數範圍。
<c:remove var="dogAge" scope="page" />
<c:catch/>
var: 例外變數名稱。
<c:catch var="ex">
<c:import value="http://java.sun.com" />
</c:catch>
邏輯條件
<c:if/>
test: 測試邏輯。var: 測試結果的變數名稱。scope: 變數的存在範圍。
<c:if test="${user.age ge 40}">
You are over the hill.
</c:if>
<c:choose/>
<c:when/>
test: 測試邏輯。
<c:otherwise/>
<c:choose>
<c:when test="${a boolean expr}">
// do something
</c:when>
<c:when test="${another boolean expr}">
// do something else
</c:when>
<c:otherwise>
// do this when nothing else
</c:otherwise>
</c:choose>
集合項目
<c:forEach/>
begin: 開始位置。end: 結束位置。step: 間隔。varStatus: 狀態變數名稱。varStatus.index: 目前位置。varStatus.count: 項目數。varStatus.first: 是否為第一個 item。varStatus.last: 是否為最後一個 item。
var: 項目變數名稱。items: 要處理的集合或陣列。
<c:forEach items="${user.languages}"
var="lang"
varStatus="status">
<c:if test="${status.first}" >
You speak these languages:
<ul>
</c:if>
<li><c:out value="${lang}" /></li>
<c:if test="${status.last}">
</ul>
</c:if>
</c:forEach>
<c:forTokens/>
begin: 開始位置。end: 結束位置。step: 間隔。varStatus: 狀態變數名稱。varStatus.index: 目前位置。varStatus.count: 項目數。varStatus.first: 是否為第一個 item。varStatus.last: 是否為最後一個 item。
var: 項目變數名稱。items: 字串資料。delims: 分隔符號。
<c:set var="users">Fred,Joe,Mary</c:set>
<c:forTokens var="name" items="${users}" delims=",">
<div><c:out value="${name}" /></div>
</c:forTokens>
URL 相關
<c:import/>
url: 網址。context: context path。var: import 的網址變數名稱。scope: 變數的存在範圍。varReader:
<c:import url="includes/header.jsp">
<c:param name="title">Hello world</c:param>
</c:import>
<c:url/>
value: 網址。context: context path。var: 產生的 url 變數名稱。scope: 變數的存在範圍。
<c:url value="editProfile.do" var="profileLnk">
<c:param name="id" value="${user.id}" />
</c:url>
<a href='<c:out value="${profileLnk}" />'>
Edit Profile
</a>
<c:redirect/>
url: 導頁網址。context: context path。
<c:if test="${empty user}">
<c:redirect url="login.do" />
</c:if>
<c:param/>
name: 參數名稱。value: 參數值。