JSP JSTL <x:choose> 标签

返回上一级

JSP 标准标签库(JSTL) <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> 没有属性

范例

webapp/jsp_jstl_xml_choose.jsp

<%@ 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" %>
<!DOCTYPE html>
<meta charset="utf-8">
<title>JSTL x:choose 标签 - JSP 基础教程 | 简单教程(www.twle.cn)</title>
<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>
<p>JSTL x:choose 标签 - JSP 基础教程 | 简单教程(www.twle.cn)</p>

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

返回上一级

JSP 基础教程

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

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

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