1、forEach标签的简单使用:

(1)未设置步长属性时,默认步长为1:

<c:forEach ">
    <c:out value="${number}"></c:out>
</c:forEach>

(2)设置步长属性后,步长改变:

<c:forEach ">
    <c:out value="${number}"></c:out>
</c:forEach>

从0到12输出,需要运用到begin和end属性。

2、遍历ArrayList集合:

(1)forEach遍历集合(ArrayList集合存储字符串):

<body>
<%
    ArrayList<String> string=new ArrayList<String>();
    string.add("星期一");
    string.add("星期二");
    string.add("星期三");
    string.add("星期四");
    string.add("星期五");
    string.add("星期六");
    string.add("星期天");
    request.setAttribute("string",string);
%>
<c:forEach items="${requestScope.string}" var="str">
    <c:out value="${str}"></c:out><br>
</c:forEach>
</body>

先向ArrayList集合中添加元素,再将ArrayList集合添加到request域,使用forEach指明要遍历的集合来自哪个域,并将每一个元素分别输出(str代表每一个数据)。

(2)遍历存储了学生对象的集合:

建立javabean:

package pers.zhb.domain;

public class Student {
    private String name;
    private int age;
    private String sex;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

遍历集合中的对象:

<body>
<%
   ArrayList<Student> students=new ArrayList<Student>();
   Student student1=new Student();
   student1.setSex("男");
   student1.setAge();
   student1.setName("mill");
   students.add(student1);

    Student student2=new Student();
    student2.setSex("女");
    student2.setAge();
    student2.setName("莉莉");
    students.add(student2);

    request.setAttribute("student",students);
%>
<c:forEach items="${requestScope.student}" var="stu">
    <c:out value="${stu}"></c:out><br>
</c:forEach>
</body>

3、遍历Map集合:

(1)键和值都为String类型:

<body>
<%
    Map<String, String> map = new HashMap<String, String>();
    map.put("河南", "郑州");
    map.put("北京", "北京");
    session.setAttribute("provincialCapital",map);
%>
<c:forEach items="${sessionScope.provincialCapital}" var="province">
    <c:out value="${province.key}:${province.value}"></c:out>
</c:forEach>
</body>

(2)遍历值为Student对象的Map集合:

<body>
<%
    ArrayList<Student> students=new ArrayList<Student>();
    Student student1=new Student();
    student1.setSex("男");
    student1.setAge();
    student1.setName("mill");
    students.add(student1);

    Student student2=new Student();
    student2.setSex("女");
    student2.setAge();
    student2.setName("莉莉");
    students.add(student2);

    Map<String, Student> map = new HashMap<String, Student>();
    map.put("student1", student1);
    map.put("student2", student2);
    session.setAttribute("studentsMap",map);
%>
<c:forEach items="${sessionScope.studentsMap}" var="student">
    <c:out value="${student.key}: ${student.value.getName()}${student.value.getSex()}${student.value.getAge()}}"></c:out><br>
</c:forEach>
</body>

forEach标签的更多相关文章

  1. MyBatis的Mapper文件的foreach标签详解

    MyBatis的Mapper文件的foreach标签用来迭代用户传递过来的Lise或者Array,让后根据迭代来拼凑或者批量处理数据.如:使用foreach来拼接in子语句. 在学习MyBatis M ...

  2. jstl foreach标签

    forEach标签 forEach标签用来循环. 属性: * var :定义循环变量 * begin :从哪开始 * end :到哪结束 * step :递增 * items :遍历的内容 * var ...

  3. MyBatis foreach标签遍历数组

    有时候开发中需要根据多个ID去查询,可以将ID封装为List或者数组然后使用MyBatis中的foreach标签构建in条件. 这里我将ID封装为String[]作为参数. <select id ...

  4. jstl 中<c:forEach />标签

    <c:forEach>标签有如下属性: 属性 描述 是否必要 默认值 items 要被循环的信息 否 无 begin 开始的元素(0=第一个元素,1=第二个元素) 否 0 end 最后一个 ...

  5. JSTL的c:forEach标签(${status.index})

    <c:forEach>标签具有以下一些属性: var:迭代参数的名称.在迭代体中可以使用的变量的名称,用来表示每一个迭代变量.类型为String. items:要进行迭代的集合.对于它所支 ...

  6. javaWeb 使用jsp开发 foreach 标签

    1.jsp代码 测试数据 <% List<String> list = new ArrayList<String>(); list.add("aaa" ...

  7. mybatis动态sql中foreach标签的使用

    foreach标签主要用于构建in条件,他可以在sql中对集合进行迭代.如下: <delete id="deleteBatch"> delete from user w ...

  8. 详解JSTL的forEach标签

    详解JSTL的forEach标签 为循环控制,它可以将集合(Collection)中的成员循序浏览一遍.      <c:forEach> 标签的语法 说明 : 语法:迭代一集合对象之所有 ...

  9. mybatis foreach标签

    一.批量插入数据 示例:添加订单商品表 1.模型层的相应代码 /** * 添加订单商品表 * @param ordergoods * @return */ public boolean addOrde ...

  10. [jstl] forEach标签使用

     在JSP的开发中,迭代是经常要使用到的操作.例如,逐行的显示查询的结果等.在早期的JSP中,通常使用Scriptlets来实现Iterator或者Enumeration对象的迭代输出.现在,通过JS ...

随机推荐

  1. Requests方法 -- 参数关联与JSESSION(上一个接口的返回数据作为下一个接口的请求参数)

    前言 参数关联是接口测试和性能测试最为重要的一个步骤,很多接口的请求参数是动态的,并且需要从上一个接口的返回值里面取出来,一般只能用一次就失效了.最常见的案例就是网站的登录案例,很多网站的登录并不仅仅 ...

  2. python中的全局变量

    1. 在函数中定义的局部变量如果和全局变量同名,则会使用局部变量(即隐藏全局变量). 示例: x = 1 def func(): x = 2 print x func() print x 运行结果: ...

  3. CodeForces 601B Lipshitz Sequence

    Lipshitz Sequence 题解: 可以通过观察得到,对于任意一个区间来说, 只有相邻的2个点的差值才会是区间的最大值. 具体观察方法,可以用数学分析, 我是通过画图得到的. 那么基于上面的观 ...

  4. CF992C Nastya and a Wardrobe 数学 第四道

    Nastya and a Wardrobe time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. WebGL2系列之采样器对象

    前言 在WebGL1中,纹理的图片和采样信息都是写在纹理对象之中. 采样信息告诉GPU如何去读取贴图上图片的信息. 如果我们希望从同一个图片多次读取像素信息,但是每次读取的时候使用的过滤方式不一样, ...

  6. Spring boot 全局配置 properties或者yml文件报错

    主要问题是没有扫描到配置文件 在pom文件里面<build>    </build>中加上以下代码就可以保证能扫描到了 <resources> <resour ...

  7. 【Offer】[21] 【调整数组顺序使奇数位于偶数前面】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,(偶数之间和奇数 ...

  8. linux中启动Zookeeper

    1.先把zookeeper的安装包解压在/usr/local,如下: 2.进入zookeeper目录,创建一个data目录 3.进入同级conf目录下,里面有zoo_sample.cfg,修改该文件名 ...

  9. mysql 复制表结构和表数据

    CREATE TABLE a1 ( id INT NOT NULL AUTO_INCREMENT COMMENT '编号', txt VARCHAR(20) NOT NULL DEFAULT '' C ...

  10. Android如何管理sqlite

    Android中使用SQlite进行数据操作 标签: sqliteandroid数据库sqlintegerstring 2012-02-28 14:21 8339人阅读 评论(2) 举报  分类: a ...