您的位置:首页 > Web前端 > JavaScript

Jsp 标签之<c:foreach>循环

2016-10-19 00:44 726 查看
jsp要使用foreach

一:前提

1:在maven中引入jstl包,并且在jsp上要引入

<%@ tagliburi=”http://java.sun.com/jsp/jstl/core”prefix=”c”%>

<%@ tagliburi=”http://java.sun.com/jstl/fmt”prefix=”fmt”%>

2:因为jsp本身不支持el表达式,所以需要声明

<%@ page isELIgnored=”false”%>

二:使用目的

我们的页面可能需要循环生成模板,这时候可以使用foreach来达到目的

以jfinal为例子

比如说我们通过查询得到两个list集合,list1,list2

我们要在list1循环内再循环list2才能得到我们需要的页面效果

parentColumn为主导航栏

sonColumn为主导航栏下的副导航栏,

2个column类包含id,parent_id.

parentColumn的parent_id=0

sonColumn的parent_id=parentColumn.id

//后台代码
list<parentColum> list1=new ArrayList<parentColum>();
List<sonColum> list2=new ArrayList<sonColum>();
setAttr("parentColums",list1);
setAttr("sonColums",list2);
render("demo.jsp");


我们在页面上

<c:foreach items="${parentColumns}" var="${column}">
<div>
<a>${colum.name}</a>
<c:foreach items="${sonColumns}" var="${sonColumn}">
<c:if test="${colum.id==sonColumn.parent_id}">
<li>${soncolum.name}</li>
</c:if>
</c:foreach>
</div>
</c:foreach>


这样就可以在页面上生成模板。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp