【发布时间】:2015-03-17 16:26:12
【问题描述】:
嗨朋友们,我正在尝试遍历包含对象列表的列表 我正在尝试遍历该列表。列表的每个元素都是一个“对象”。 我在 c:forEach 的帮助下访问了那个“对象”的变量。我得到了价值观。但一次又一次的相同。 我不知道在迭代它们时将对象添加到 servlet 或 jsp 中的列表时是否犯了任何错误。
这是我的 servlet 代码
FileInfo fileInfo = new FileInfo();
List<FileInfo> fileInfoList = new ArrayList<FileInfo>();
HashMap<String, String> uploadHistory = new HashMap<String, String>();
while(rs.next()) {
fileInfo.setFile_id(rs.getString("file_id"));
fileInfo.setFile_name(rs.getString("file_name"));
fileInfo.setUpload_time(rs.getString("upload_time"));
fileInfo.setUsername(rs.getString("username"));
fileInfoList.add(fileInfo);
uploadHistory.put(rs.getString("file_name"),rs.getString("upload_time"));
}
request.setAttribute("uploadHistory",uploadHistory);
request.setAttribute("fileInfo",fileInfo);
request.setAttribute("fileInfoList", fileInfoList);
RequestDispatcher rd = request.getRequestDispatcher("/UploadHistoryJsp");
rd.forward(request, response);
这是我的jsp
<div class="panel-body">
<table id="uploadHistoryTable" class="table">
<thead>
<tr>
<th><strong>File Name</strong></th>
<th><strong>Date & Time</strong></th>
</tr>
</thead>
<tbody>
<c:forEach var="uploaded" items="${fileInfoList}">
<tr>
<td><a href="${pageContext.request.contextPath}/DownloadServlet?f=${uploaded.file_name}&p=${uploaded.username}">${uploaded.file_name}</a></td>
<td>${uploaded.upload_time}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div><!-- panel-body -->
请帮忙。如有任何混淆,请见谅。
【问题讨论】:
-
您总是只更新
FileInfo的一个实例。只需将这一行FileInfo fileInfo = new FileInfo();移动到 while 循环中,以便在将新对象添加到 map 之前添加一个新对象 -
@Arkantos 是对的……这就是解决方案。