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
廣告