Java 如何在 .JSP 文件中显示列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18085476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a list in a .JSP file?
提问by DeaIss
After an hour of solid research I still can't do this.
经过一个小时的扎实研究,我仍然无法做到这一点。
This is my Servlet code:
这是我的 Servlet 代码:
package com.fdm.ProjectWeb.RedirectServlets;
import java.awt.List;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import com.fdm.ProjectWeb.Controller.ValidateRegisterInputController;
import com.fdm.ProjectWeb.Model.OraclePullListOfUsers;
import com.fdm.ProjectWeb.Model.OracleUserManagement;
public class VerifyRedirect extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
OraclePullListOfUsers pull = new OraclePullListOfUsers();
ResultSet rs = pull.unverifiedUsers();
List list = new List();
try {
while (rs.next()){
list.add(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("list", list);
RequestDispatcher rd = req.getRequestDispatcher("./WEB-INF/VerifyUser.jsp");
rd.forward(req, resp);
}
}
And this is my .JSP code:
这是我的 .JSP 代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Verify Users</title>
</head>
<body>
<table>
<c:forEach items="${list}" var="item">
<tr>
<td><c:out value="${item}" /></td>
</tr>
</c:forEach>
</table>
<h2>Please enter the Username of the user you want to verify</h2>
<form action="loginform" method="POST">
<label>User To Verify: <input type="text" name="userToVerify" id="userToVerify" /></label><br />
<input type="submit" value="Submit" name="submit" />
</form>
</body>
The Result Set definitely has data in it as if I system.out.println in the while loop it shows all the right values.
结果集里面肯定有数据,就好像我在 while 循环中的 system.out.println 一样,它显示了所有正确的值。
And I get this error message:
我收到此错误消息:
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
Any help would be appreciated!
任何帮助,将不胜感激!
采纳答案by Rohit Jain
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
javax.servlet.jsp.JspTagException:不知道如何迭代 <forEach> 中提供的“项目”
This exception occur when your <c:forEach items>
does not refer to an Object, that can be iterated upon. The Objectshould either be Iterable
, a Map
, or an array.
So clearly, your listattribute refers to the type which does not come under any of the above category. Though the type is actually a List
, but not java.util.List
.
当您<c:forEach items>
不引用可以迭代的Object 时,会发生此异常。的对象应或者是Iterable
,一个Map
,或一个阵列。
很明显,您的列表属性指的是不属于上述任何类别的类型。虽然类型实际上是 a List
,但不是java.util.List
。
Check your import statement:
检查您的导入语句:
import java.awt.List; // Here is the fault
It should be:
它应该是:
import java.util.List;
Also, you should use generic type List
instead of raw type. Change:
此外,您应该使用泛型类型List
而不是原始类型。改变:
List list = new List();
to:
到:
List<String> list = new List<String>();
Also, it seems like you are doing the pre-processingtask in doPost()
method. Don't. doPost()
is used for post-processinga request. You should use doget()
method for pre-processing.
此外,您似乎正在执行方法中的预处理任务doPost()
。别。doPost()
用于对请求进行后处理。您应该使用doget()
方法进行预处理。
Move all your code in doPost()
to doGet()
method.
将所有代码doPost()
移到doGet()
方法中。
回答by Benoit Wickramarachi
Change:
改变:
List list = new List();
To:
到:
List<String> list = new ArrayList<String>();
From
从
java.util.List;
回答by benny
try instantiating the list like this:
尝试像这样实例化列表:
List<String> list = new ArrayList<String>();
or if it does not help maybe supply the list as an array such as:
或者如果它没有帮助,可以将列表作为数组提供,例如:
req.setAttribute("list", list.toArray());