JSTL基本标签一 ForEach

来源:互联网 发布:贵州省大数据 编辑:程序博客网 时间:2024/04/28 05:22
在前面可以看到EL表达式,是可以代替script去获取request,session,application等里面的数据的。但是如果我们想要遍历比如一个数组,但是又不想用script那么这个时候就是需要用到JSTL(JSP 标准标记库)

1.在只需要把jstl.jar 引入到我们的lib中。

2. 页面引入 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> (prefix="c" 这个名字有我们自由取,一般取c表示的是 core , uri  表示的TLD文件。)

我们就在jsp中可以使用JSTL

在jsp页面通常使用EL 和 JSTL基本上都已经足够了。


JSTL 基本的标签

  1. 迭代器<c:forEach> (循环 List Array)  
 后台代码:
String[] values = new String[]{"zhangsan","lisi","wangwu"};req.setAttribute("values", values);

 页面:
<c:forEach var="value" items="${values}" step="1" varStatus="status" begin="0" end="1">${value}   ${status.count }<br /></c:forEach>

说明:
        1.${values} 表示的req中的values 
        2.var 表示定义的局部的变量
        3.step表示的步长,1 表示的是i++ , 其他值你懂的
        4.begin 默认从0开始
        5.end 表示结束的位置
        6.varStatus 
        7.${values}可以是数组,可以是List对象
        8.c:forEach  是可以嵌套的使用的


下面这张图片很好的说明了这个对应的关系


<c:forEach> 也是可以嵌套来使用的 比如下面这个例子
List<String[]> values = new ArrayList<String[]>();for(int index = 0 , count = 10 ; index < count ; index++ ){values.add(new String[]{ "zhangsan" + index});}

jsp页面使用

<c:forEach var="value" items="${values}" step="1" varStatus="status" begin="0"  > <c:forEach var="v" items="${value}"> ${v } </c:forEach></c:forEach>




0 0
原创粉丝点击