JSP JSTL <c:choose>, <c:when> 标签

返回上一级

JSP 标准标签库(JSTL) <c:choose> 标签与 Java switch 语句的功能一样,用于在众多选项中做出选择

switch 语句中有 case,而 <c:choose>标签中对应有 <c:when>
switch 语句中有 default,而 <c:choose> 标签中有 <c:otherwise>

语法

<c:choose> 语法格式如下

<c:choose>
    <c:when test="<boolean>">
        ...
    </c:when>
    <c:when test="<boolean>">
        ...
    </c:when>
    ...
    ...
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

属性

  • <c:choose> 标签没有属性

  • <c:when> 标签只有一个属性,在下表中有给出

  • <c:otherwise>标签没有属性

<c:when> 标签的属性如下:

属性 描述 必须 默认值
test 条件

范例

webapp/jstl_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" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
    table { border-collapse: collapse; }
    table,th,td {border:1px solid #ddd;}
    th,td {padding:5px 10px;text-align: left}
</style>
<title>JSTL &lt;c:choose&gt; 标签 - JSP 基础教程 | 简单教程(www.twle.cn)</title>
<c:set var="salary" scope="session" value="${2000*2}"/>
<p>你的工资为 : <c:out value="${salary}"/></p>
<c:choose>
    <c:when test="${salary <= 0}">
       太惨了。
    </c:when>
    <c:when test="${salary > 1000}">
       不错的薪水,还能生活。
    </c:when>
    <c:otherwise>
        什么都没有。
    </c:otherwise>
</c:choose>
<p>JSTL &lt;c:choose&gt; 标签 - JSP 基础教程 | 简单教程(www.twle.cn)</p>

在浏览器上输入 http://localhost:8080/jsp/jstl_c_choose.jsp 显示如下

返回上一级

JSP 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.