jsp中<c:foreach>没有遍历
代码如下,topic为一个实体bean,有author属性,为说明起见,author赋0,1,2值,但是发现<c:forEach>中没有遍历<%List<Topic>li...
代码如下,topic为一个实体bean,有author属性,为说明起见,author 赋0,1,2值,但是发现 <c:forEach>中没有遍历
<%
List<Topic> list = new ArrayList<Topic>();
Topic topic = new Topic();
for(int i= 0 ;i <3;i++)
{
topic.setAuthor(i+"");
list.add(topic);
System.out.println("author:" + topic.getAuthor());
}
request.setAttribute("topicTest",list);
%>
<c:forEach items="${requestScope.topicTest}" var="topic">
${topic.author}<br>
</c:forEach>
原因已找到:Topic topic = new Topic(); 应放到for 循环中去,要不然的话,之后的数据会覆盖之前的数据 展开
<%
List<Topic> list = new ArrayList<Topic>();
Topic topic = new Topic();
for(int i= 0 ;i <3;i++)
{
topic.setAuthor(i+"");
list.add(topic);
System.out.println("author:" + topic.getAuthor());
}
request.setAttribute("topicTest",list);
%>
<c:forEach items="${requestScope.topicTest}" var="topic">
${topic.author}<br>
</c:forEach>
原因已找到:Topic topic = new Topic(); 应放到for 循环中去,要不然的话,之后的数据会覆盖之前的数据 展开
1个回答
展开全部
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<%
List<String> list = new ArrayList<String>();
String topic = new String("hello way");
for(int i= 0 ;i <3;i++)
{
topic += (i +"");
list.add(topic);
System.out.println("author:" + topic);
}
request.setAttribute("topicTest",list);
%>
<c:forEach items="${requestScope.topicTest}" var="topic">
${topic}<br>
</c:forEach>
</body>
</html>
输出
This is my JSP page.
hello way0
hello way01
hello way012
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<%
List<String> list = new ArrayList<String>();
String topic = new String("hello way");
for(int i= 0 ;i <3;i++)
{
topic += (i +"");
list.add(topic);
System.out.println("author:" + topic);
}
request.setAttribute("topicTest",list);
%>
<c:forEach items="${requestScope.topicTest}" var="topic">
${topic}<br>
</c:forEach>
</body>
</html>
输出
This is my JSP page.
hello way0
hello way01
hello way012
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询