JSP JSTL <x:when> 标签
JSP 标准标签库(JSTL) <x:when> 相当于 Java when 语句中的 case 语句
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>
属性
属性 | 描述 | 是否必要 | 默认值 |
---|---|---|---|
select | 条件 | 是 | 无 |
范例
webapp/jstl_xml_when.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:when 标签 - 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:when 标签 - JSP 基础教程 | 简单教程(www.twle.cn)</p>
在浏览器上输入 http://localhost:8080/jsp/jstl_xml_when.jsp 显示如下