如何在.JSP文件中显示列表? [英] How to display a list in a .JSP file?

查看:151
本文介绍了如何在.JSP文件中显示列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一个小时的坚实研究后,我仍然无法做到这一点。

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

这是我的Servlet代码:

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);
    }
}

这是我的.JSP代码:

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>

结果集中肯定有数据,就像我在while循环中的system.out.println一样显示所有正确的值。

The Result Set definitely has data in it as if I system.out.println in the while loop it shows all the right values.

我收到此错误消息:

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

任何帮助都将不胜感激!

Any help would be appreciated!

推荐答案


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

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

< c:forEach items> 未引用 Object <时,会发生此异常/ em>,可以迭代。 Object 应为 Iterable Map 数组

很明显,您的列表属性是指不属于上述任何类别的类型。虽然类型实际上是 List ,但不是 java.util.List

This exception occur when your <c:forEach items> does not refer to an Object, that can be iterated upon. The Object should either be Iterable, a Map, or an array.
So clearly, your list attribute 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.

检查您的import语句:

Check your import statement:

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

它应该是:

import java.util.List;

此外,您应该使用泛型类型列表而不是原始类型。更改:

Also, you should use generic type List instead of raw type. Change:

List list = new List();

to:

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






此外,您好像在做 c> doPost()方法中的预处理任务。别。 doPost()用于后期处理 a申请。您应该使用 doget()方法进行预处理


Also, it seems like you are doing the pre-processing task in doPost() method. Don't. doPost() is used for post-processing a request. You should use doget() method for pre-processing.

移动所有你的代码在 doPost() doGet()方法。

Move all your code in doPost() to doGet() method.

这篇关于如何在.JSP文件中显示列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆