Jsp-jstl-xml-choose-tag

提供:Dev Guides
移動先:案内検索

JSTL-XML <x:choose>、<x:when>、<x:otherwise>タグ

*<x:choose>* タグは、Java switchステートメントのように機能します。 これにより、いくつかの選択肢から選択できます。 switchステートメントにcaseステートメントがある場合、 *<x:choose>* タグには *<x:when>* タグがあります。 同様に、switchステートメントにはデフォルトアクションを指定するためのデフォルト句があり、 *<x:choose>* タグにはデフォルト句として *<x:otherwise>* タグがあります。

属性

  • <x:choose> タグには属性がありません。
  • <x:when> タグには、以下にリストする属性が1つあります。
  • <x:otherwise> タグには属性がありません。

<x:when>タグには次の属性があります-

Attribute Description Required Default
select Condition to evaluate Yes None

<%@ 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 Tags</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>

次の結果が表示されます-

Books Info:
Book is written by ZARA