我有一个带有 Flight
的 POJO,其中只有一些私有(private)类变量以及 getter 和 setther,并且我的 servlet 中有以下代码:
request.setAttribute("FLIGHT_LIST", flightList);
request.getRequestDispatcher("/user_panel.jsp").forward(request, response);
以及下面的jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="entities.Flight"%>
<%@ page import="java.util.List"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>Hello im user</h3>
<div>
<a href="login.jsp">Login</a> | <a href="/Airport/LogoutServlet">Logout</a>
| <a href="/Airport/UserProfile">Profile</a>
</div>
<hr>
<div>
<table style="width: 100%" border="1">
<tr>
<th>ID</th>
<th>Number</th>
<th>Type</th>
<th>Departure City</th>
<th>Departure Time</th>
<th>Arrival City</th>
<th>Arrival Time</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
</tr>
<c:forEach var="element" items="${FLIGHT_LIST}">
<tr>
<td><c:out value="${element.idflight}" /></td>
<td><c:out value="${element.number}" /></td>
<td><c:out value="${element.type}" /></td>
<td><c:out value="${element.departureCity}" /></td>
<td><c:out value="${element.departureTime}" /></td>
<td><c:out value="${element.arrivalCity}" /></td>
<td><c:out value="${element.arrivalTime}" /></td>
</tr>
</c:forEach>
</table>
</div>
<hr>
....
</body>
</html>
问题是它没有按照我预期的方式工作。它在表中打印值 ${...}
。我该如何解决这个问题?
最佳答案
我觉得您没有正确配置 jSTL,因此 for 循环的行为方式不正确。尝试如下所示的简单 forEach 看看您的设置是否有效。
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
另请参阅
关于java - JSP 打印数组列表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422455/