c:foreach的用法

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="java.util.*,com.jspdev.ch3.TestBean"%>
<%
   Collection users=new ArrayList();
   for(int i=0;i<5;i++)
   {
      TestBean user=new TestBean();
      user.setUserName("guest"+i);
      user.setPassword("guest1"+i);
         user.setAge(20+i);
      users.add(user);
   }
   session.setAttribute("users",users);
%>
<html>
<head>
  <title>JSTL:c:forEach的使用</title>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>User list</h4>
<table border=1>
<tr><td>用户名</td><td>密码</td><td>年龄</td></tr>
<c:forEach var="users" items="${users}">
  <tr>
  <td><c:out value="${users.userName}"/></td>
  <td><c:out value="${users.password}"/></td>
  <td><c:out value="${users.age}"/></td>
  </tr>
</c:forEach>
   </table>   
</center></body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:c:forEach的使用</title>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>第二种迭代:1 to 10</h4>

<c:forEach var="i" begin="1" end="10">
  <c:out value="${i}"/> -->
</c:forEach>
<h4>第二种迭代:1 to 10,step=3</h4>
<c:forEach var="i" begin="1" end="10" step="3">
  <c:out value="${i}"/> -->
</c:forEach>

</center>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="java.util.*,com.jspdev.ch3.TestBean"%>
<html>
<head>
  <title>JSTL:c:forEach的使用</title>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>User list</h4>
<table border=1>
<tr><td>用户名</td><td>密码</td><td>年龄</td></tr>
<c:forEach var="users" items="${users}" begin="2" end="4" step="2">
  <tr>
  <td><c:out value="${users.userName}"/></td>
  <td><c:out value="${users.password}"/></td>
  <td><c:out value="${users.age}"/></td>
  </tr>
</c:forEach>
</table>
</center>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="java.util.*,com.jspdev.ch3.TestBean"%>
<html>
<head>
  <title>JSTL:c:forEach的使用</title>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>User list</h4>
<table border=1>     
<tr><td>用户名</td><td>密码</td><td>年龄</td><td>index</td><td>count</td><td>first?</td><td>last?</td></tr>
<c:forEach var="users" items="${users}" varStatus="status">
  <tr>  
  <td><c:out value="${users.userName}"/></td>
  <td><c:out value="${users.password}"/></td>
  <td><c:out value="${users.age}"/></td>
  <td><c:out value="${status.index}"/></td>
  <td><c:out value="${status.count}"/></td>
  <td><c:if test="${status.first}">
     <b></c:if>
      <c:out value="${status.first}"/></b></td>
  <td><c:if test="${status.last}">
     <i> </c:if>
     <c:out value="${status.last}"/></i></td>   
  </tr>
  
</c:forEach>
</table>
</center>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page import="java.util.*"%>
<html>
<head>
  <title>JSTL: Iterator Support -- Data Types Example</title>
</head>
<body bgcolor="#FFFFFF">
<h3>Data Types</h3>
<%
 int[] myIntArray=new int[]{1,2,3,4,5,65,34};
 String[] myStringArray=new String[]{"I ","am ","a ","Java","fans"};
 Vector v=new Vector();
 v.add("this");
 v.add("is");
 v.add("myEnumeration");
 v.add("!");
 Enumeration myEnumeration=v.elements();
 HashMap myNumberMap=new HashMap();
 myNumberMap.put("hellking","23");
 myNumberMap.put("guest","23");
 myNumberMap.put("guest2","223");
 myNumberMap.put("guest3","232");
request.setAttribute("myIntArray",myIntArray);
request.setAttribute("myStringArray",myStringArray);
request.setAttribute("myEnumeration",myEnumeration);
request.setAttribute("myNumberMap",myNumberMap);
%>
<h4>Array of primitives (int)</h4>

<c:forEach var="i" items="${myIntArray}">
  <c:out value="${i}"/> ?
</c:forEach>

<h4>Array of objects (String)</h4>

<c:forEach var="string" items="${myStringArray}">
  <c:out value="${string}"/><br>
</c:forEach>

<h4>myEnumeration (warning: this only works until myEnumeration is exhausted!)</h4>

<c:forEach var="item" items="${myEnumeration}" begin="0" end="5" step="1">
  <c:out value="${item}"/><br>
</c:forEach>

<h4>Properties (Map)</h4>

<c:forEach var="prop" items="${myNumberMap}" begin="1" end="5">
  <c:out value="${prop.key}"/> = <c:out value="${prop.value}"/><br>
</c:forEach>

<h4>String (Common Separated Values)</h4>

<c:forEach var="token" items="red,blue,green">
  <c:out value="${token}"/><br>
</c:forEach>
</body>
</html>


顺便介绍一款上网加速器利器,程序员必备!自己一直在用,有需要的朋友可以试试!
http://honx.in/i/VIFTbc6vD2Sxigzt

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值