JSTL <x:choose>,<x:when>,<x:otherwise>標簽
<x:choose>就像一個Java的switch語句,它可以讓你在一些替代之間作選擇。switch語句中有case語句,<x:choose>標簽具有<x:when>標簽。switch語句中有默認的子句來指定一個默認的行為,類似方式如:<x:choose>有<x:otherwise>作為default語句。
屬性:
-
<x:choose>標簽冇有任何屬性。
-
<x:when>標簽有一個屬性,將在下文列出。
-
<x:otherwise> 標簽冇有任何屬性。
<x:when>標簽具有以下屬性:
屬性 | 描述 | 必須 | 默認 |
---|---|---|---|
select | 條件評估 | Yes | None |
例子:
<%@ 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 |