<x:choose>, <x:when>, <x:otherwise> 标签

<x:choose>标签与Java switch语句有相同的功能。switch语句有case语句,而<x:choose>标签有<x:when>标签。switch语句有default语句,而<x:choose>标签有<x:otherwise>标签。

语法格式

  1. <x:choose>
  2. <x:when select="<string>">
  3. ...
  4. </x:when>
  5. <x:when select="<string>">
  6. ...
  7. </x:when>
  8. ...
  9. ...
  10. <x:otherwise>
  11. ...
  12. </x:otherwise>
  13. </x:choose>

属性

  • 没有属性。
  • 的属性在下表中给出。
  • 没有属性。

<x:when>标签的属性:

属性描述是否必要默认值
select条件

实例演示

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  4. <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
  5. <html>
  6. <head>
  7. <title>JSTL x:choose 标签</title>
  8. </head>
  9. <body>
  10. <h3>Books Info:</h3>
  11. <c:set var="xmltext">
  12. <books>
  13. <book>
  14. <name>Padam History</name>
  15. <author>ZARA</author>
  16. <price>100</price>
  17. </book>
  18. <book>
  19. <name>Great Mistry</name>
  20. <author>NUHA</author>
  21. <price>2000</price>
  22. </book>
  23. </books>
  24. </c:set>
  25. <x:parse xml="${xmltext}" var="output"/>
  26. <x:choose>
  27. <x:when select="$output//book/author = 'ZARA'">
  28. Book is written by ZARA
  29. </x:when>
  30. <x:when select="$output//book/author = 'NUHA'">
  31. Book is written by NUHA
  32. </x:when>
  33. <x:otherwise>
  34. Unknown author.
  35. </x:otherwise>
  36. </x:choose>
  37. </body>
  38. </html>

运行结果如下:

  1. BOOKS INFO:
  2. Book is written by ZARA