我有一个列表,假设它包含以下元素:{a,b,c,d,e,f},我想在两个jsp页面上显示它们列(使用,不是表),以这种方式
A B
C D
E F
我认为是这样的事情,但它不起作用:
<c:forEach var="item" items="${list}" varStatus="loopCounter">
<div>
<div>${item.key} <b>${item.value}</b></div>
</div>
<c:if test="${loopCounter.count%2 ==0}">
<BR>
</c:if>
</c:forEach>
您说${list}
是列表,
但是您尝试像使用${item.key}
和${item.value}
的地图一样访问它。
我想你是在混合两者,因此您没有得到您的期望。
下面是两个示例,其中有列表和地图,如何显示表格(如表):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>table with modulo</title>
<style type="text/css">
.psydoTableCell {
float:left;
margin-left:10px;
display: table-cell;
}
</style>
</head>
<body>
<%
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
pageContext.setAttribute("list", list);
%>
<h3>iterate a list</h3>
<div>
<c:forEach var="item" items="${list}" varStatus="loopCounter">
<div class="psydoTableCell">${item}</div>
<c:if test="${loopCounter.count %2 == 0}">
<br>
</c:if>
</c:forEach>
</div>
<%
Map<String,String> map =new HashMap<String,String>();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
map.put("d", "4");
pageContext.setAttribute("map", map);
%>
<h3>iterate a map</h3>
<div>
<c:forEach var="item" items="${map}" varStatus="loopCounter">
<div class="psydoTableCell">${item.key}</div>
<div class="psydoTableCell">${item.value}</div>
<br>
</c:forEach>
</div>
</body>
</html>
输出:
迭代列表
a b
C D
E F迭代地图
a 1
B 2
C 3
D 4