<x:set>標簽為XPath表達式的值設(shè)置一個變量。
如果XPath表達式的值是boolean類型,則<x:set>將會設(shè)置一個java.lang.Boolean對象,若是字符串,則設(shè)置一個java.lang.String對象,若是數(shù)字,則設(shè)置一個java.lang.Number對象。
<x:set var="<string>" select="<string>" scope="<string>"/>
<x:set>標簽有如下屬性:
| 屬性 | 描述 | 是否必要 | 默認值 |
|---|---|---|---|
| var | 代表XPath表達式值得變量 | 是 | Body |
| select | 需要計算的XPath表達式 | 否 | 無 |
| scope | var屬性的作用域 | 否 | Page |
接下來的例子告訴我們?nèi)绾问褂?lt;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>
<h2>Books Info:</h2>
<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>
運行結(jié)果如下:
BOOKS INFO: The price of the second book:[[book: null], [book: null]]