Jstl表示式out、set、if、choose、forEach
阿新 • • 發佈:2019-02-08
JSP標準標籤庫(JSTL)是一個JSP標籤集合,它封裝了JSP應用的通用核心功能。
下載jakarta-taglibs-standard-1.1.2.zip 包並解壓,將jakarta-taglibs-standard-1.1.2/lib/下的兩個jar檔案:standard.jar和jstl.jar檔案拷貝到/WEB-INF/lib/下。
核心標籤是最常用的JSTL標籤。引用核心標籤庫的語法如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out> 用於在JSP中顯示資料,就像<%= ... >
<c:set> 用於儲存資料
<c:remove> 用於刪除資料
<c:catch> 用來處理產生錯誤的異常狀況,並且將錯誤資訊儲存起來
<c:if> 與我們在一般程式中用的if一樣
<c:choose> 本身只當做<c:when>和<c:otherwise>的父標籤
<c:when> <c:choose>的子標籤,用來判斷條件是否成立
<c:otherwise> <c:choose>的子標籤,接在<c:when>標籤後,當<c:when>標籤判斷為false時被執行
<c:import> 檢索一個絕對或相對 URL,然後將其內容暴露給頁面
<c:forEach> 基礎迭代標籤,接受多種集合型別
<c:forTokens> 根據指定的分隔符來分隔內容並迭代輸出
<c:param> 用來給包含或重定向的頁面傳遞引數
<c:redirect> 重定向至一個新的URL.
<c:url> 使用可選的查詢引數來創造一個URL
Customer
package com.safly;
public class Customer {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Customer() {
super();
}
}
我們來看看c:out、c:set的簡單用法吧:
c:out語法格式
<c:out value="<string>" default="<string>" escapeXml="<true|false>"/>
屬性 描述 是否必要 預設值
value 要輸出的內容 是 無
default 輸出的預設值 否 主體中的內容
escapeXml 是否忽略XML特殊字元 否 true
c:set語法格式
<c:set
var="<string>"
value="<string>"
target="<string>"
property="<string>"
scope="<string>"/>
屬性 描述 是否必要 預設值
value 要儲存的值 否 主體的內容
target 要修改的屬性所屬的物件 否 無
property 要修改的屬性 否 無
var 儲存資訊的變數 否 無
scope var屬性的作用域 否 Page
例項程式碼如下:
<%
request.setAttribute("book", "java");
%>
book:
<c:out value="${requestScope.book}"></c:out>
<c:set var="name" value="safly" scope="page"></c:set>
<!-- 等價於下面一行程式碼,為域物件賦值 -->
<%
pageContext.setAttribute("name", "safly");
%>
name:${pageScope.name }
<%
Customer cust = new Customer();
cust.setId(101);
request.setAttribute("cust", cust);
%>
ID:${requestScope.cust.id }
<c:set target="${requestScope.cust}" property="id" value="${param.id}"></c:set>
ID:${requestScope.cust.id }
book: java name:safly ID:101 ID:234
我們來看看c:if、c:choose標籤的簡單用法吧:
c:choose語法格式
<c:choose>
<c:when test="<boolean>"/>
...
</c:when>
<c:when test="<boolean>"/>
...
</c:when>
...
...
<c:otherwise>
...
</c:otherwise>
</c:choose>
<c:choose>標籤沒有屬性。
<c:when>標籤只有一個屬性,在下表中有給出。
<c:otherwise>標籤沒有屬性。
c:if語法格式
<c:if test="<boolean>" var="<string>" scope="<string>">
...
</c:if>
屬性 描述 是否必要 預設值
test 條件 是 無
var 用於儲存條件結果的變數 否 無
scope var屬性的作用域 否 page
例項程式碼如下:
<c:set value="20" var="age" scope="request"></c:set>
<c:if test="${requestScope.age > 18}">成年了</c:if>
<c:if test="${param.age > 18}" var="isAdult" scope="request"></c:if>
isAdult:<c:out value="${requestScope.isAdult}"></c:out><br>
<c:choose>
<c:when test="${param.age > 60}">老年</c:when>
<c:when test="${param.age > 40}">中年</c:when>
<c:otherwise>青年</c:otherwise>
</c:choose>
輸出結果如下:
成年了 isAdult:true
中年
我們來看看c:forEach標籤的簡單用法吧:
c:forEach語法格式
<c:forEach
items="<object>"
begin="<int>"
end="<int>"
step="<int>"
var="<string>"
varStatus="<string>">
...
屬性 描述 是否必要 預設值
items 要被迴圈的資訊 否 無
begin 開始的元素(0=第一個元素,1=第二個元素) 否 0
end 最後一個元素(0=第一個元素,1=第二個元素) 否 Last element
step 每一次迭代的步長 否 1
var 代表當前條目的變數名稱 否 無
varStatus 代表迴圈狀態的變數名稱 否 無
<%
List<Customer> custs = new ArrayList<Customer>();
custs.add(new Customer(1,"AA"));
custs.add(new Customer(2,"BB"));
custs.add(new Customer(3,"CC"));
custs.add(new Customer(4,"DD"));
custs.add(new Customer(5,"EE"));
custs.add(new Customer(6,"FF"));
request.setAttribute("custs",custs);
%>
<c:forEach items="${requestScope.custs}" var="safly" begin="1" step="2" end="5">
${safly.id }:${safly.name }
</c:forEach>
<br>
<%
Map<String,Customer> custMap = new HashMap<String,Customer>();
custMap.put("a",new Customer(1,"AA"));
custMap.put("b",new Customer(2,"BB"));
custMap.put("c",new Customer(3,"CC"));
custMap.put("d",new Customer(4,"DD"));
custMap.put("e",new Customer(5,"EE"));
request.setAttribute("custMap",custMap);
%>
<c:forEach items="${requestScope.custMap}" var="safly">
${safly.key }---${safly.value.id }--${safly.value.name }<br>
</c:forEach>
<%
String [] names = new String[]{"A","B","C"};
request.setAttribute("names",names);
%>
<c:forEach var="safly" items="${names}">
${safly }
</c:forEach>
2:BB 4:DD 6:FF
d---4--DD
e---5--EE
b---2--BB
c---3--CC
a---1--AA
A B C