Latest web development tutorials

<x:set> 標籤

JSP 標準標籤庫 JSP標準標籤庫

<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 標準標籤庫 JSP標準標籤庫