当前位置:首页 > JSP教程 > JSP高级教程

jstl<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 评估条件 None

文件:c_choose.jsp -

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>c:choose 示例</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <c:set var="salary" scope="session" value="${5600*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 > 5000}">
            Salary is very good.
         </c:when>

            <c:otherwise>
            No comment sir...
         </c:otherwise>
        </c:choose>
    </div>

</body>
</html>

执行上面示例代码,得到以下输出结果 -

Your salary is : 11200 
Salary is very good.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!