Latest web development tutorials

<C: escolha>, <c: when>, <c: otherwise> tag

JSP Standard Tag Library JSP Standard Tag Library

<C: escolha> tag e função Java switch como fazer uma escolha em um número de opções.

switch tem um caso, mas <c: choose> tag tem um correspondente <c: when>, instrução switch com um padrão, e <c: choose> tag no <c: otherwise>.

sintaxe

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

propriedade

  • <C: escolha> tag não possui atributos.
  • <C: quando> marca tem apenas um atributo, têm dado na tabela a seguir.
  • <C: otherwise> tag não possui atributos.

<C: quando> propriedades de tag da seguinte forma:

propriedade descrição Se necessário omissão
teste condição é não

Exemplos Demonstração

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:choose 标签实例</title>
</head>
<body>
<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>
</body>
</html>

Os resultados são como se segue:

你的工资为 : 4000

不错的薪水,还能生活。

JSP Standard Tag Library JSP Standard Tag Library