如何遍历 jsp 中的对象列表
How to iterate through list of objects in jsp
嗨,朋友们,我正在尝试遍历包含对象列表的列表
我正在尝试遍历该列表。列表的每个元素都是 'object'。
我在 c:forEach 的帮助下访问 'object' 的变量。我得到了价值观。但同样一次又一次。
我不知道在将对象添加到 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 = new FileInfo();
在 while
循环内:
while(rs.next()) {
FileInfo fileInfo = new FileInfo();
....
照原样,您只创建一个实例并不断更新它。您很可能会多次看到从数据库中提取的最后一个元素。
您一次又一次地添加相同的对象。您需要在 while 循环
中启动 FileInfo
while(rs.next()) {
FileInfo fileInfo= new FileInfo();
.....
fileInfoList.add(fileInfo)
}
您的代码的问题是您总是通过 fileInfo
变量更新 FileInfo 的单个实例,因此在每次迭代中您只是覆盖单个对象的状态。
FileInfo fileInfo = new FileInfo();
--------
while(rs.next()) {
// updating fileInfo from result set
}
将 FileInfo
移动到 while 循环中,如下所示
while(rs.next()) {
FileInfo fileInfo = new FileInfo();
// update new fileInfo from result set and add to fileInfoList
}
嗨,朋友们,我正在尝试遍历包含对象列表的列表 我正在尝试遍历该列表。列表的每个元素都是 'object'。 我在 c:forEach 的帮助下访问 'object' 的变量。我得到了价值观。但同样一次又一次。 我不知道在将对象添加到 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 = new FileInfo();
在 while
循环内:
while(rs.next()) {
FileInfo fileInfo = new FileInfo();
....
照原样,您只创建一个实例并不断更新它。您很可能会多次看到从数据库中提取的最后一个元素。
您一次又一次地添加相同的对象。您需要在 while 循环
中启动FileInfo
while(rs.next()) {
FileInfo fileInfo= new FileInfo();
.....
fileInfoList.add(fileInfo)
}
您的代码的问题是您总是通过 fileInfo
变量更新 FileInfo 的单个实例,因此在每次迭代中您只是覆盖单个对象的状态。
FileInfo fileInfo = new FileInfo();
--------
while(rs.next()) {
// updating fileInfo from result set
}
将 FileInfo
移动到 while 循环中,如下所示
while(rs.next()) {
FileInfo fileInfo = new FileInfo();
// update new fileInfo from result set and add to fileInfoList
}