Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 350|回复: 0

[JSTL学习]jstl之foreach循环

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:57:24 | 显示全部楼层 |阅读模式
    <%@ page contentType="text/HTML; charset=GBK" %>
    <%@ page import="java.util.*" %>

    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
    <html>
       <head>
         <title>JSTL测试1--c:forEach循环</title>
       </head>

       <body>
    一、整数
         <c:forEach var="i" begin="1" end="10" step="1">
           <c:out value="$ {i}" />,
         </c:forEach><p>
      
      
       
       
       

       
      
      二、计算x的平方
        <table>  
        <tr><th>Value</th>  
         <th>Square</th></tr>  
         <c:forEach var="x" begin="0" end="10" step="2">  
         <tr><td><c:out value="$ {x}"/></td>  
             <td><c:out value="$ {x*x}"/></td></tr>  
        </c:forEach>  
       </table> <p>
    三、字符串"47,52,53,55,46,22,16,2" 分隔.
        <table border="1">
        <c:forTokens items="47,52,53,55,46,22,16,2" delims="," var="dailyPrice">
         <tr><td><c:out value="$ {dailyPrice}"/></td></tr>
        </c:forTokens>
        </table><p>
    四、使用步长
        <table>  
         <tr><th>second</th>  
         <th>second</th></tr>  
         <c:forEach var="seconds" begin="0" end="${pageContext.session.maxInactiveInterval}" step="60">  
         <tr><td><c:out value="$ {seconds}"/></td>  
             <td><c:out value="$ {seconds}"/></td></tr>  
        </c:forEach>  
       </table> <p>
    五、对数组进行循环<p>
       <% int ai[] = {10, 20, 30, 40, 50};
       pageContext.setAttribute("ary", ai);
       %>
       <c:forEach var="i" items="$ {ary}">
         <c:out value="$ {i}"/>*
       </c:forEach><p>

      <%
        Cookie c=new Cookie("cookie1","one");
        response.addCookie(c);
      %>

      <%  
       Cookie cookies[]=request.getCookies();  
       Cookie sCookie=null;   
       String sname=null;  
       String name=null;  
       if(cookies==null) // 如果没有任何cookie  
         out.print("none any cookie");  
       else  
       {  
         //out.print(cookies.length + "<br>");  
         for(int i=0;i<cookies.length; i++) // 循环列出所有可用的Cookie  
         {  
          sCookie=cookies;  
           sname=sCookie.getName();  
           name = sCookie.getValue();  
           out.println(sname + "->" + name + "<br>");  
         }  
       }   
    %>  
       
    <table border="1" align="center">  
        <tr><th>Cookie Name</th>  
         <th>Cookie Value</th></tr>  
         <c:forEach var="cook" items="${pageContext.request.cookies}">  
         <tr><td><c:out value="$ {cook.name}"/></td>  
             <td><c:out value="$ {cook.value}"/></td></tr>  
        </c:forEach>  
       </table> <p>

    六、字符串数组循环
      <% String as[] = {
                 "A first string", "La deuxieme string", "Ella troisiemo stringo"
             };
             request.setAttribute("stringArray", as);
      %><p>
      <c:forEach var="string" items="${stringArray}">
       <c:out value="$ {string}"/><br>
      </c:forEach>
    七、枚举
      <%
      Hashtable hashtable1 = new Hashtable();
                pageContext.setAttribute("numberMap", hashtable1);
          
             hashtable1.put(new Integer(1), "uno");
             hashtable1.put(new Integer(2), "dos");
             hashtable1.put(new Integer(3), "tres");
             hashtable1.put(new Integer(4), "cuatro");
             hashtable1.put(new Integer(5), "cinco");
             hashtable1.put(new Integer(6), "seis");
             hashtable1.put(new Integer(7), "siete");
             hashtable1.put(new Integer(8), "ocho");
             hashtable1.put(new Integer(9), "nueve");
             hashtable1.put(new Integer(10), "diez");

             java.util.Enumeration enumeration = hashtable1.keys();
             pageContext.setAttribute("enumeration", enumeration);
    %>

        <c:forEach var="item" items="$ {enumeration}" begin="2" end="10" step="2">
          <c:out value="$ {item}"/><br>
        </c:forEach><p>
    八、map<p>
        <c:forEach var="prop" items="$ {numberMap}" begin="1" end="5">
          <c:out value="$ {prop.key}"/> = <c:out value="$ {prop.value}"/><br>
        </c:forEach>

       

      </body>
    </html>

      

      
      
       
       

         
       

         
       
      


      

      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:27 , Processed in 0.297831 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表