Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình

Cách áp dụng tag choose trong JSP

Tag <c:choose> hoạt động như câu lệnh switch của Java: chọn giữa nhiều lựa chọn. Switch có case, <c:choose> có <c:when>. Switch có default, <c:choose> có <c:otherwise>.

Thuộc tính

  • <c:choose> không có attribute.
  • <c:when> có 1 attribute.
  • <c:otherwise> không có attribute.
AttributeDescriptionRequiredDefault
testĐiều kiện đánh giáYesNone

Ví dụ

<%@ taglib uri = "https://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>

Kết quả:

Your salary is : 4000
Salary is very good.