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. CF Edu54 E. Vasya and a Tree DFS+树状数组

    Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...

  2. poj 3169 Layout(差分约束+spfa)

    题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...

  3. mysql 主主从配置

    配置主服务器:主服务器1 Ip:  192.168.0.1 主服务器2 Ip:  192.168.0.2 主服务器1配置 2.1.修改mysql配置文件 vim /etc/my.conf Server ...

  4. 模板类型推导、auto推导

    effective modern c++ 果然是神书,干货满满,简单记录下. item1 模板推倒 典型的模板函数 temlate<class T> void fn(ParamType p ...

  5. 基于Selenium+Python的web自动化测试框架

    一.什么是Selenium? Selenium是一个基于浏览器的自动化测试工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.S ...

  6. text2pcap: 将hex转储文本转换为Wireshark可打开的pcap文件

    简介 Text2pcap是一个读取ASCII hex转储的程序,它将描述的数据写入pcap或pcapng文件.text2pcap可以读取包含多个数据包的hexdumps,并构建多个数据包的捕获文件.t ...

  7. 【深入浅出-JVM】(77):SPI

    概念 Service Provider Interface 规则 在resource/META-INF/services 创建一个以接口全限定名为命名的文件,内容写上实现类的全限定名 接口实现类在cl ...

  8. ScrollView内嵌ViewPager导致ViewPager滑动困难问题

    转自:http://titanseason.iteye.com/blog/1858874 解决方式:重写ScrollView,然后在xml中定义布局的时候,使用自定义的PagerScrollView而 ...

  9. xshell使用小技巧

    1. 方便复制:Tool -->  options --> right buttion(paste the clipboard contents) and copy selected te ...

  10. application.properties 乱码 (已验证)

    1.打开Eclipse或MyEclipse 2.选择window-Preferences-content Types-Text-Java Properties File 3.将Java Propert ...