<x:choose>, <x:when>, <x:otherwise>-Tags

JSP 标准标签库JSP-Standard-Tag-Bibliothek

Das <x:choose>-Tag hat dieselbe Funktionalität wie die Java-switch-Anweisung. Die switch-Anweisung hat case-Anweisungen, während das <x:choose>-Tag <x:when>-Tags hat. Die switch-Anweisung hat eine default-Anweisung, während das <x:choose>-Tag ein <x:otherwise>-Tag hat.

Syntaxformat

<x:choose>
 <x:when select="<string>">
     ...
 </x:when>
 <x:when select="<string>">
     ...
 </x:when>
     ...
     ...
 <x:otherwise>
     ...
 </x:otherwise>
</x:choose>

Attribute

  • <x:choose> hat keine Attribute.
  • Die Attribute von <x:when> sind in der folgenden Tabelle angegeben.
  • <x:otherwise> hat keine Attribute.

Attribute des <x:when>-Tags:

Attribute Beschreibung Erforderlich Standardwert
select Bedingung Ja Keine
 

Beispiel-Demonstration

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>
<head>
  <title>JSTL x:choose 标签</title>
</head>
<body>
<h3>Books Info:</h3>

<c:set var="xmltext">
  <books>
    <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
    </book>
    <book>
      <name>Great Mistry</name>
      <author>NUHA</author>
      <price>2000</price>
    </book>
  </books>
</c:set>

<x:parse xml="${xmltext}" var="output"/>
<x:choose>
   <x:when select="$output//book/author = 'ZARA'">
      Book is written by ZARA
   </x:when>
   <x:when select="$output//book/author = 'NUHA'">
      Book is written by NUHA
   </x:when>
   <x:otherwise>
      Unknown author.
   </x:otherwise>
</x:choose>

</body>
</html>

Die Ausgabe sieht wie folgt aus:

BOOKS INFO:
Book is written by ZARA

JSP 标准标签库JSP-Standard-Tag-Bibliothek

Andere Erweiterungen