jstl迭代标签
程序员文章站
2022-04-23 13:11:04
...
一 定义
二 代码
<%@ page language="java" import="java.util.*,com.cakin.domain.*" pageEncoding="utf-8" %>
<!-- 引入jstl标签库-->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html>
<head>
<title>My JSP 'c_out.jsp' starting page</title>
</head>
<body>
<%
ArrayList<Rat> a1=new ArrayList<Rat>();
Rat rat=new Rat();
rat.setName("小宝");
rat.setAge(2);
Rat rat1=new Rat();
rat1.setName("大宝");
rat1.setAge(4);
Rat rat2=new Rat();
rat2.setName("老宝");
rat2.setAge(8);
a1.add(rat);
a1.add(rat1);
a1.add(rat2);
request.setAttribute("all",a1);
request.setAttribute("abc","12;34;你好;haoma");
%>
<h1>传统迭代</h1>
<%
ArrayList<Rat> a12=(ArrayList<Rat>)request.getAttribute("all");
for(Rat rat3:a12)
{
out.println(rat3.getName()+" "+rat3.getAge());
}
%>
<h1>迭代标签c:foreach</h1>
<c:forEach items="${all}" var="rat">
<c:out value="${rat.name}"></c:out>
<c:out value="${rat.age}"></c:out>
</c:forEach>
<h1>第二种迭代 1 to 10</h1>
<c:forEach var="i" begin="1" end ="10">
<c:out value="${i}"></c:out>
</c:forEach>
<h1>第三种迭代 1 to 10 step=3</h1>
<c:forEach var="i" begin="1" end ="10" step="3">
<c:out value="${i}"></c:out>
</c:forEach>
<h1>fortokens</h1>
<c:forTokens items="12;34;你好" delims=";" var="temp">
${temp}
</c:forTokens>
<br>
<c:forTokens items="${abc}" delims=";" var="temp">
${temp}
</c:forTokens>
</body>
</html>
三 测试结果