JSTL学习笔记

JSP Standard Tag Library (JSTL) 的规范完成于2002年7月,随后Apache Taglibs Projects在不久的几天中提交了一个参考实现。

JSTL 的出现是为了解决程序员一直渴望有一个标准的标签库的需求,同时也为开发JSP带来了很大的便利。

JSTL 1.0提供了一系列基于JSP 1.2 API的标签库,下表列举了一些标签库的信息:

DescriptionPrefixDefault 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的支持的操作符:
OperatorDescription
. 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 TypeDescription
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.
支持的内建对象:
VariableDescription
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根目录,非常实用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值