JSTL 學習筆記

JSP Standard Tag Library (JSTL) 的規範完成於2002年7月,隨後Apache Taglibs Projects在不久的幾天中提交了一個參考實現。

JSTL 的出現是爲了解決程序員一直渴望有一個標準的標籤庫的需求,同時也爲開發JSP帶來了很大的便利。

JSTL 1.0提供了一系列基於JSP 1.2 API的標籤庫,下表列舉了一些標籤庫的信息:
Description Prefix Default URI
Core c http://java.sun.com/jstl/core
XML Processing x http://java.sun.com/jstl/xml
I18N & Formatting fmt http://java.sun.com/jstl/fmt
Database Access sql http://java.sun.com/jstl/sql

如果要使用JSTL,那麼需要加入如下聲明段:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

除了標籤庫之外,JSTL 1.0還定義了一個所謂的
Expression Language (EL)表達式語言。
EL用於訪問運行時數據而出現,如果學過JavaScript,你會發現EL在表達上與其很類似。
${myObj.myProperty}$
${myObj["myProperty"]}$
${myObj[varWithTheName]}$

以上的語句用來檢索一個對象的內部值,都是等價的。如果是訪問一個數組或者列表:
${myList[2]}$
${myList[aVar + 1]}$

EL的支持的操作符:
Operator Description
. Access a property
[] Access an array/list element
() Group a subexpression
+ Addition
- Subtraction or negation of a number
/ or div Division
% or mod Modulo (remainder)
== or eq Test for equality
!= or ne Test for inequality
< or lt Test for less than
> or gt Test for greater than
<= or le Test for less than or equal
>= or gt Test for greater than or equal
&& or and Test for logical AND
|| or or Test for logical OR
! or not Unary Boolean complement
empty Test for empty value (null, empty string, or an empty collection)
支持的字面量:
Literal Type Description
String Enclosed with single or double quotes. A quote of the same type within the string must be escaped with backslash: (/' in a string enclosed with single quotes; /" in a string enclosed with double quotes). The backslash character must be escaped as // in both cases.
Integer An optional sign (+ or -) followed by digits between 0 and 9.
Floating Point The same as an integer literal, except that a dot is used as the separator for the fractional part and an exponent can be specified as e or E, followed by an integer literal.
Boolean true or false.
Null null.
支持的內建對象:
Variable Description
param A collection of all request parameters as a single string value for each parameter.
paramValues A collection of all request parameters as a string array value for each parameter.
header A collection of all request headers as a single string value for each header.
headerValues A collection of all request headers as a string array value for each header.
cookie A collection of all request cookies as a single javax.servlet.http.Cookie instance value for each cookie.
initParams A collection of all application init parameters as a single string value for each parameter.
pageContext An instance of the javax.servlet.jspPageContext class.
pageScope A collection of all page scope objects.
requestScope A collection of all request scope objects.
sessionScope A collection of all session scope objects.
applicationScope A collection of all application scope objects.

如果你要訪問GET參數:
${param.listType}
如果你要訪問HTTP頭信息:${header['User-Agent']}

訪問Session或者Request內部包含對象:
${sessionScope.customer}
${requestScope.customer}

一些例子:
First name: <c:out value="${customer.firstName}" />
<c:out value="First name: ${customer.firstName}" />
First name: <c_rt:out value="<%= customer.getFirstName() %>" />
都是等價的。

控制流程和迭代操作也是JSTL的一個特性,個人認爲
迭代標籤是當Java 1.5沒出來之前對於Java語言的最好補充。
迭代操作使用的forEach標籤:
<c:forEach items="${forecasts.rows}" var="city">
City: <c:out value="${city.name}" />
Tomorrow's high: <c:out value="${city.high}" />
Tomorrow's low: <c:out value="${city.low}" />
</c:forEach>

Choose標籤:
<c:choose>
<c:when test="${param.first > 0}">
<a href="foreach.jsp?first=<c:out value="${param.first - noOfRows}"/>">
Previous Page</a>
</c:when>
<c:otherwise>
Previous Page
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${param.first + noOfRows < forecasts.rowsCount}">
<a href="foreach.jsp?first=<c:out value="${param.first + noOfRows}"/>">
Next Page</a>
</c:when>
<c:otherwise>
Next Page
</c:otherwise>
</c:choose>
URL操作:
<c:url var="previous" value="foreach.jsp">
<c:param name="first" value="${param.first - noOfRows}" />
</c:url>

特別提及的URL操作:
<c:url value="/images/logo.gif" />
<a href="<c:out value="${previous}"/>">Previous Page</a>
這裏的"/images/logo.gif"不是從網站的根目錄起始,而是自動調整到所對應的context根目錄,非常實用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章