JSTL - XML <x:set> 標籤



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

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

屬性

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

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

示例

以下示例將演示如何使用 <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]]
jsp_standard_tag_library.htm
廣告

© . All rights reserved.