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


<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 条件
 

实例演示

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
  3.  
  4. <html>
  5. <head>
  6. <title>JSTL x:choose 标签</title>
  7. </head>
  8. <body>
  9. <h3>Books Info:</h3>
  10.  
  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.  
  26. <x:parse xml="${xmltext}" var="output"/>
  27. <x:choose>
  28. <x:when select="$output//book/author = 'ZARA'">
  29. Book is written by ZARA
  30. </x:when>
  31. <x:when select="$output//book/author = 'NUHA'">
  32. Book is written by NUHA
  33. </x:when>
  34. <x:otherwise>
  35. Unknown author.
  36. </x:otherwise>
  37. </x:choose>
  38.  
  39. </body>
  40. </html>

运行结果如下:

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