我遇到了一个jsp不显示数据的常见问题。然而,我遵循文档,也尝试了解决在这个网站上提出的许多问题,但没有任何成功。
如何在jsp中显示java.io.file数据?如果操作正确,是否需要任何其他配置?
我有一个使用java.io.file的文件[],它是在javax.servlet.http.httpservlet中创建的。然后,我获取文件[],并将其作为属性添加到我的httpsession中。然后将其转发到jsp文件,传递到foreach标记,并填充到表中((见本文)
这是我的java servlet:
public class LogManagement extends HttpServlet {
private static final Logger logger = LoggerFactory.getLogger(LogManagement.class);
private static final String BASE_DIRECTORY = System.getProperty("catalina.base");
private static final File CURRENT_LOGS_FOLDER = new File(BASE_DIRECTORY + "/logs");
private static final File CAPTURED_LOGS_FOLDER = new File(BASE_DIRECTORY + "/logs/captured");
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("GET called on LogManagement servlet");
try {
getLogs(request);
getServletContext().getRequestDispatcher("/LogManagement.jsp").forward(request, response);
} catch (Exception e) {
logger.info("ERROR unable to GET log data");
logger.info(e.getMessage());
}
}
private void getLogs(HttpServletRequest request) {
logger.info("Getting logs");
File[] currentLogs = CURRENT_LOGS_FOLDER.listFiles();
File[] capturedLogs = CAPTURED_LOGS_FOLDER.listFiles();
HttpSession session = request.getSession();
session.setAttribute("currentLogs", currentLogs);
session.setAttribute("capturedLogs", capturedLogs);
logger.info("Got logs");
}
}
这是我在这里和这里尝试的两个解决方案的jsp(是的,我知道它使用了一些不推荐的标记属性,但我无法更改这些属性)
解决方案1:
<table style="margin-top: 5px; width: 600px;" align="left">
<tr>
<td align="left">
Current Logs:
</td>
</tr>
<tr>
<td>
<div class="file-viewer">
<table>
<c:forEach var="file" items="${currentLogs}">
<tr>
<td><c:out value="${file.name}"/></td>
<td><c:out value="${file.length}"/></td>
<td><c:out value="${file.lastModified}"/></td>
</tr>
</c:forEach>
</table>
</div>
</td>
</tr>
</table>
解决方案2:
<table style="margin-top: 5px; width: 600px;" align="left">
<tr>
<td align="left">
Captured Logs:
</td>
</tr>
<tr>
<td>
<div class="file-viewer">
<table>
<c:forEach var="file" items="${capturedLogs}">
<tr>
<td>${file.name}</td>
<td>${file.length}</td>
<td>${file.lastModified}</td>
</tr>
</c:forEach>
</table>
</div>
</td>
</tr>
</table>
这是我在文档和各种so答案中看到的两种解决方案。我也试过了 <% out.print(file.getName()); %>
td标记的内部没有运气(请参阅无法在表中显示jsp数据)
这是我得到的一个例外:
一次异常处理发生了一次异常处理过程(log管理.jsp)一次异常处理一次异常处理一次异常处理一次异常处理一次异常处理一次异常处理一次异常处理一次异常处理一次异常处理一次异常处理一次[log管理.jsp)一次在线[51]一[51]一[51]一次[51]一次[51]一[51]一[48::::uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu0.18]一[51]在线在线[51]在线[51]在线[51]在线[51]48:::::::::;48:::::::48::::::48::::::::::::::::::::::::::::uuuuuuuuuuuutd\uuuuu c:out value=${file.lastmodified}//td\uuuuu 53:\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu/truuuuuuuuuuuuuuuuuuuuuu54:\uuuuuu
如果我用虚拟数据替换我要查找的数据,我会得到每个项目的打印输出。
<td>fileName</td>
<td>fileSize</td>
<td>lastEdited</td>
给出一个表,其中的行数等于我正在查看的文件数,显示:
“文件名文件大小上次编辑”
这表明我的foreach工作正常,但对特定数据的引用不正常。
1条答案
按热度按时间j2cgzkjk1#
看来你错过了比赛
c:out
标签,就像你在上面循环时插入的一样currentLogs
.作为一个旁注,除非它是一个大学课程或遗留项目的要求,在2021中学习JSP是不值得的。