我正在尝试在 .jsp 文件中显示实体列表,但出现此错误:
Unable to compile class for JSP:
An error occurred at line: 28 in the jsp file: /guestbook.jsp
Can only iterate over an array or an instance of java.lang.Iterable
25: </tr>
26: <tr>
27: <%
28: for (Entity drugtarget : "${drugtargets}") {
29: %>
30: <td>
31: <%
这是我在 servlet 类中的查询:
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query = new Query("DrugTarget").addSort("drug", Query.SortDirection.DESCENDING);
List<Entity> drugtargets = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10));
req.setAttribute("drugtargets", drugtargets);
这就是我将drugtargets发送到jsp文件的方式:
String url = "/guestbook.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
try {
rd.forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
}
最后是我如何编写jsp部分:
<table border="1">
<tr>
<th>Drug Names</th>
<th>Target Names</th>
</tr>
<tr>
<%
for (Entity drugtarget : "${drugtargets}") {
%>
<td>
<%
pageContext.setAttribute("drug_content",
drugtarget.getProperty("drug"));
%>
${fn:escapeXml(drug_content)}
</td>
<td>
<%
pageContext.setAttribute("target_content",
drugtarget.getProperty("target"));
%>
${fn:escapeXml(target_content)}
</td>
<%
}
%>
</tr>
</table>
我想我犯了语法错误,但我不确定。我检查了一些示例,并使用了相同的方式(将实体列表从 servlet 发送到 jsp 并到达 jsp 文件中的实体)。如何解决此错误?