JSTL - Core <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 要評估的條件

示例

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
   <head>
      <title><c:choose> Tag Example</title>
   </head>

   <body>
      <c:set var = "salary" scope = "session" value = "${2000*2}"/>
      <p>Your salary is : <c:out value = "${salary}"/></p>
      <c:choose>
         
         <c:when test = "${salary <= 0}">
            Salary is very low to survive.
         </c:when>
         
         <c:when test = "${salary > 1000}">
            Salary is very good.
         </c:when>
         
         <c:otherwise>
            No comment sir...
         </c:otherwise>
      </c:choose>
   
   </body>
</html>

以上程式碼將生成以下結果:

Your salary is : 4000
Salary is very good. 
jsp_standard_tag_library.htm
廣告