5

After an hour of solid research I still can't do this.

This is my Servlet code:

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:

<%@ 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.

And I get this error message:

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;

Any help would be appreciated!

4

3 回答 3

9

javax.servlet.jsp.JspTagException:不知道如何迭代 <forEach> 中提供的“项目”

<c:forEach items>当您没有引用可以迭代的Object时,会发生此异常。Object应该是、IterableaMaparray
很明显,您的列表属性指的是不属于上述任何类别的类型。虽然类型实际上是 a List,但不是java.util.List

检查您的导入声明:

import java.awt.List;   // Here is the fault

它应该是:

import java.util.List;

此外,您应该使用泛型类型List而不是原始类型。改变:

List list = new List();

至:

List<String> list = new List<String>();

此外,您似乎正在方法中执行预处理任务doPost()。不。doPost()用于对请求进行后处理。您应该使用doget()方法进行预处理

将所有代码doPost()移到doGet()方法中。

于 2013-08-06T16:25:49.890 回答
2

改变:

List list = new List();

至:

List<String> list = new ArrayList<String>();

java.util.List;
于 2013-08-06T16:28:12.047 回答
1

尝试像这样实例化列表:

List<String> list = new ArrayList<String>();

或者如果它没有帮助,可以将列表作为数组提供,例如:

req.setAttribute("list", list.toArray());
于 2013-08-06T16:37:24.550 回答