English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

<x:choose> , <x:when> , <x:otherwise> タグ

JSP スタンダードタグライブラリ

<x:choose>タグはJavaのswitch文と同じ機能を持っています。switch文にはcase文があり、<x:choose>タグには<x:when>タグがあります。switch文にはdefault文があり、<x:choose>タグには<x:otherwise>タグがあります。

文法形式

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

属性

  • <x:choose>属性はありません。

  • <x:when>の属性は以下のテーブルに示されています。

  • <x:otherwise>属性はありません。

<x:when>タグの属性:

属性描述是否必要默认值
select 条件

 

示例演示

<%@ 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>
<h2>Books Info:</h2>
<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>

実行結果は以下の通りです:

BOOKS INFO:
Book is written by ZARA

JSP スタンダードタグライブラリ