<x:set> 標籤
<x:set>標籤為XPath運算式的值設置一個變數。
如果XPath運算式的值是boolean類型,則<x:set>將會設置一個java.lang.Boolean對象,若是字串,則設置一個java.lang.String對象,若是數字,則設置一個java.lang.Number對象。
語法格式
<x:set var="<string>" select="<string>" scope="<string>"/>
屬性
<x:set>標籤有如下屬性:
| 屬性 | 描述 | 是否必要 | 默認值 |
|---|---|---|---|
| var | 代表XPath運算式值得變數 | 是 | Body |
| select | 需要計算的XPath運算式 | 否 | 無 |
| scope | var屬性的作用域 | 否 | Page |
實例演示
接下來的例子告訴我們如何使用<x:set>標籤:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>JSTL x:set 標籤</title>
</head>
<body>
<h3>Books Info:</h3>
<c:set var="xmltext">
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
</c:set>
<x:parse xml="${xmltext}" var="output"/>
<x:set var="fragment" select="$output//book"/>
<b>The price of the second book</b>:
<c:out value="${fragment}" />
</body>
</html>
運行結果如下:
BOOKS INFO: The price of the second book:[[book: null], [book: null]]

JSP 標準標籤庫