如何在 JSP 中將 XPath 表示式結果的值儲存到變數中?


<x:set> 標籤將變數設定為 XPath 表示式的值。

如果 XPath 表示式結果為布林值,則 <x:set> 標籤設定一個 java.lang.Boolean 物件;對於字串,則設定 java.lang.String 物件;對於數字,則設定 java.lang.Number 物件。

屬性

<x:set> 標籤具有以下屬性:

屬性描述必填預設值
var設定為 XPath 表示式值的變數主體
select要評估的 XPath 表示式
scopevar 屬性中指定的變數的作用域頁面

示例

以下示例將演示如何使用 <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 Tags</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>

訪問上述 JSP,將顯示以下結果:

Books Info:
The price of the second book:[[book: null], [book: null]]

更新於: 2019-07-30

176 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告