zoukankan      html  css  js  c++  java
  • jstl标签forEach的用法--遍历java的集合

    再讲<c:forEach>之前,现讲一下让EL表达式生效的语句

    <% @ page isELIgnored="false"%>这句语句在你想让EL表达式生效的情况下,必须要加载jsp中。

    <c:forEach>中各个常用属性的解释如下:

    items:要遍历的集合,通常用EL表达式表示:pageContext.setAttribute("mylist",list); ${mylist}

    var:变量

    varStatus:变量的状态  varStatus的属性有index,和count.其中index从0开始数起,count:个数。

    下面的代码讲了java中常用的集合:

     1 <%@ page import="java.util.ArrayList" %>
     2 <%@ page import="com.supwisdom.domain.Student" %>
     3 <%@ page import="java.util.HashMap" %>
     4 <%@ page language="java" contentType="text/html; charset=UTF-8"
     5          pageEncoding="UTF-8"%>
     6 <%--el表达式生效的语句--%>
     7 <%@page isELIgnored="false" %>
     8 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     9 <%--防止调用函数时报错--%>
    10 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
    11 <html>
    12 <body>
    13 <h2>Hello World!</h2>
    14 <%--forEach的用法--%>
    15 <%--用forEach遍历list集合--%>
    16 <%
    17             ArrayList<String> strings = new ArrayList<String>();
    18             strings.add("王昭君");
    19             strings.add("西施");
    20             strings.add("霍去病");
    21             strings.add("杨开慧");
    22             pageContext.setAttribute("ouyangfeng",strings);
    23 %>
    24 <c:forEach var="item" items="${ouyangfeng}" varStatus="status">
    25     <c:out value="${item}"></c:out>
    26    status.index: <c:out value="${status.index}"></c:out>
    27     status.count:<c:out value="${status.count}"></c:out><br>
    28 </c:forEach>
    29 <%--遍历list对象的集合--%>
    30 <%
    31     Student student = new Student();
    32     student.setId(1);
    33     student.setName("zyh");
    34     student.setAge(16);
    35     Student student2 = new Student();
    36     student2.setId(2);
    37     student2.setName("ouyangfeng");
    38     student2.setAge(29);
    39     ArrayList<Student> students = new ArrayList<Student>();
    40     students.add(student);
    41     students.add(student2);
    42     pageContext.setAttribute("plump",students);
    43 %>
    44 
    45 <c:forEach var="item" items="${plump}">
    46     学生的姓名:  <c:out value="${item.name}"></c:out>
    47     学生的年龄:  <c:out value="${item.age}"></c:out>
    48     学生的身份证号:<c:out value="${item.id}"></c:out>
    49     <br>
    50 </c:forEach>
    51 <%--遍历list数组集合--%>
    52 <%
    53     ArrayList<String[]> sixi = new ArrayList<String[]>();
    54    String[] str= new String[]{"country","china"};
    55     String[] str2=new String[]{"address","NewYork"};
    56    String[] str3= new String[]{"student","pupil"};
    57     sixi.add(str);
    58     sixi.add(str2);
    59     sixi.add(str3);
    60     pageContext.setAttribute("array",sixi);
    61 %>
    62 <c:forEach var="item" items="${array}">
    63     <c:out value="${item[0]}"></c:out> <%--数组中的第一个元素--%>
    64     <c:out value="${item[1]}"></c:out>  <%--数组中的第二个元素--%>
    65     <br>
    66 </c:forEach>
    67 <%--遍历map集合--%>
    68 <%
    69     HashMap<String, String> map = new HashMap<String, String>();
    70     map.put("China","Reservation");
    71     map.put("France","Romantic");
    72     map.put("America","Innovation");
    73     pageContext.setAttribute("hashmap",map);
    74 %>
    75 <c:forEach var="item" items="${hashmap}">
    76     键是:<c:out value="${item.key}"></c:out>
    77     值是:<c:out value="${item.value}"></c:out><br>
    78 </c:forEach>
    79 </body>
    80 </html>

    tomcat运行的结果为:

     1 Hello World!
     2 
     3 王昭君 status.index: 0 status.count:1
     4 西施 status.index: 1 status.count:2
     5 霍去病 status.index: 2 status.count:3
     6 杨开慧 status.index: 3 status.count:4
     7 学生的姓名: zyh 学生的年龄: 16 学生的身份证号:1 
     8 学生的姓名: ouyangfeng 学生的年龄: 29 学生的身份证号:2 
     9 country china 
    10 address NewYork 
    11 student pupil 
    12 键是:France 值是:Romantic
    13 键是:America 值是:Innovation
    14 键是:China 值是:Reservation
  • 相关阅读:
    3d smoke(CPU版)
    Rendering of Translucent Object With PRT(原创)
    Loren Carpenter与分形山脉
    WWW.GAMEDEV.NET的每日一图
    3d fire(CPU版)
    CentOs安装Docker
    关于Reporting Service的一些零碎整理
    .NET高级调试策略
    基于云的商务智能应该注意的事项
    outlook联系人操作手册
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/7200964.html
Copyright © 2011-2022 走看看