JSTL標簽應用指南(一)
一:如何使用標簽
???????? 在jsp頁面中使用JSTL標簽之前,我們首先需要在該頁頂部聲明標簽庫的prefix(固定前綴)和URI(統一資源標識).我們要使用core JSTL庫,那么我們就要使用標準的前綴(c).例如:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>????????
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
二:表達式語言語法
???JSTL表達式${data}指明變量的范圍,名稱data.另外表達語言還支持以下固有的對象.
范圍對象 |
????????? 含義 |
pageScope
|
頁面范圍變量 |
requestScope
|
請求范圍變量 |
sessionScope
|
會話范圍變量 |
applicationScope
|
應用程序范圍變量 |
param
|
字符串類型格式的請求參數 |
paramValues
|
字符串集合類型的請求參數? |
header
|
字符串類型的HTTP請求頭部 |
headerValues
|
字符串集合類型的HTTP請求頭部 |
initParam
|
上下文相關的初始化參數 |
cookie
|
Cookie值 |
pageContext
|
當前頁的頁上下文對象 |
????????????? 例如:表達式
${param.username} 指明:請求范圍.參數名稱.
????????????其中:范圍對象為:param;username為param對象內部的一個屬性,對應于請求的一個參數。
????????????
???三:屬性訪問
??????為了從集合中得到屬性,JSTL表達式支持以下的操作:??
???????????? 1.點操作符標志命名屬性,表達式${user.iq}表示:取出user對象的iq屬性值;
???????????????The dot (
.
) operator retrieves a named property. The expression
${user.iq}
??????????????? indicates the iq property of the scoped variable named user;
???????????? 2.使用[]操作符,可以使用序號檢索出對應的屬性的名字;
???????????????The bracket (
[]
) operator lets you retrieve named or numbered properties;
???????????? 3.表達式${user["iq"]}和表達式${user.iq}含義一樣;
???????????????The expression
${user["iq"]}
has the same meaning as
${user.iq}
???????????? 4.表達式${row[0]}取出行集合中的第一個元素(第一行);
???????????????The expression
${row[0]}
indicates the first item in the
row
collection.