位置:首页 » 文章/教程分享 » JSTL x:set标签

<x:set>标签设置一个变量的XPath表达式的值。

如果XPath表达式的结果布尔值,<x:set>设置一个java.lang.Boolean的对象,如果是一个字符串,则java.lang.String,或是一个数字则为java.lang.Number。

属性:

<x:set>标签具有以下属性:

属性 描述 必须 默认
var 被设置为XPath表达式的值的变量 Yes Body
select XPath表达式进行计算 No None
scope 在var属性指定的变量范围 No 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 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]]