<x:choose>標簽與Java switch語句有相同的功能。switch語句有case語句,而<x:choose>標簽有<x:when>標簽。switch語句有default語句,而<x:choose>標簽有<x:otherwise>標簽。
<x:choose> <x:when select="<string>"> ... </x:when> <x:when select="<string>"> ... </x:when> ... ... <x:otherwise> ... </x:otherwise> </x:choose>
<x:choose>沒有屬性。
<x:when>的屬性在下表中給出。
<x:otherwise>沒有屬性。
<x:when>標簽的屬性:
| 屬性 | 描述 | 是否必要 | 默認值 |
|---|---|---|---|
| select | 條件 | 是 | 無 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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 標簽</title>
</head>
<body>
<h2>Books Info:</h2>
<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>運行結(jié)果如下:
BOOKS INFO: Book is written by ZARA