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


<c:choose>标签与Java switch语句的功能一样,用于在众多选项中做出选择。switch语句中有case,而<c:choose>标签中对应有<c:when>,switch语句中有default,而<c:choose>标签中有<c:otherwise>。

属性

  • <c:choose>标签没有属性。
  • <c:when>标签只有一个属性,在下表中有给出。
  • <c:otherwise>标签没有属性。
<c:when>标签的属性如下:
属性 描述 是否必要 默认值
test 条件

实例演示

  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  2. <html>
  3. <head>
  4. <title>c:choose 标签实例</title>
  5. </head>
  6. <body>
  7. <c:set var="salary" scope="session" value="${2000*2}"/>
  8. <p>Your salary is : <c:out value="${salary}"/></p>
  9. <c:choose>
  10. <c:when test="${salary <= 0}">
  11. Salary is very low to survive.
  12. </c:when>
  13. <c:when test="${salary > 1000}">
  14. Salary is very good.
  15. </c:when>
  16. <c:otherwise>
  17. No comment sir...
  18. </c:otherwise>
  19. </c:choose>
  20. </body>
  21. </html>

运行结果如下:

  1. Your salary is : 4000
  2. Salary is very good.