x_foreach.jsp

来自「jsp入门级代码」· JSP 代码 · 共 47 行

JSP
47
字号
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head><title>JSTL: XML -- forEach</title></head>
<body>
<h3>forEach实例</h3>
<x:parse var="xmlText">
  <books>
   <book>
    <name>JSP2.0</name>
    <publisher>清华出版社</publisher>
    <price>48</price>
    <authors>
    	<author id="1">
    		<name>Johnson</name>
    		<address>Johnson@hotmail.com</address>
    	</author>
    	<author id="2">
    		<name>Tom</name>
    		<address>Tom@hotmail.com</address>
      </author>
    </authors>
   </book>
  </books>
</x:parse>

<x:forEach select="$xmlText/books">
	&nbsp;&nbsp;<x:out select="."/><br>
</x:forEach>
<x:forEach select="$xmlText//book">
	&nbsp;&nbsp;<x:if select=".//author">
		            <x:out select=".//author/name" />
		          </x:if><br>
</x:forEach>
<x:forEach select="$xmlText//book">
  &nbsp;&nbsp;<x:choose>
  	           <x:when select='$xmlText//author[@id="2"]'>
  	           	<x:out select='$xmlText//author[@id="2"]'/>
  	           </x:when>
  	           <x:otherwise>
  	           	no author's id=2
  	           </x:otherwise>
  	          </x:choose>
</x:forEach>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?