JSP 中可以使用 XPath 表达式的 switch 语句吗?


<x:choose> 标签的工作方式类似于 Java 的 switch 语句。通过它,您可以从多个备选项中进行选择。switch 语句有 case 语句,而 <x:choose> 标签则有 <x:when> 标签。类似地,switch 语句有 default 子句来指定默认操作,而 <x:choose> 标签则有 <x:otherwise> 标签作为默认子句。

属性

  • <x:choose> 标签没有任何属性。

  • <x:when> 标签有一个属性,如下所示。

  • <x:otherwise> 标签没有任何属性。

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

属性描述必填默认值
select要评估的条件

示例

<%@ 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:choose 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:choose>
         <x:when select = "$output//book/author = 'ZARA'">
            Book is written by ZARA
          </x:when>
          <x:when select = "$output//book/author = 'NUHA'">
             Book is written by NUHA
          </x:when>
          <x:otherwise>
             Unknown author.
          </x:otherwise>
       </x:choose>
   </body>
</html>

将显示以下结果:

Books Info:
Book is written by ZARA

更新于:2019年7月30日

308 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.