2

嗨,在过去的 3 个小时里,我一直被这个错误困扰,当有人找出问题所在时,我可能会打自己一巴掌。我真的只是想将一个 ArrayList 设置为我的 TeacherAccount 类的一个实例。在运行我认为可以的最简单测试时,我不断收到以下错误,这也在下面。

这是 TeacherAccount 类

package com.studentOrganizer;


import java.util.List;

public class TeacherAccount {

    private String username;
    private String password;
    private List<String> students;

    public TeacherAccount(){
        username=null;
        password=null;
        students = null;

    }
    public TeacherAccount(String username, String password,List<String> students){
        this.username = username;
        this.password = password;
        this.students = students;
    }

    public void setUsername(String username){
        this.username = username;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public void setStudents(List<String> students){
        this.students = students;  
    }

    public String getUsername(){
        return username;
    }

    public String getPassword(){
        return password;
    }

    public List<String> getStudents() {
        return students;  
    }
}

这是我正在运行的产生错误的小测试

            TeacherAccount currentTeach = new TeacherAccount();
        List<String> tempStu = new ArrayList<String>();
        tempStu.add("Hi");
        tempStu.add("Hi again");
        currentTeach.setStudents(tempStu);

我已经缩小了 setStudents 的问题范围。tempStu 和学生都是 List 类型的任何帮助表示赞赏。谢谢

编辑这里是请求的 JSP 文件:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% String currentUsername = (String)request.getAttribute("currentUsername"); %>
<% String currentPassword = (String)request.getAttribute("currentPassword"); %>
<% String students = (String)request.getAttribute("students"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="css/style.css">
<script src="js/StudentOrganizer.js" type="text/javascript"></script>
<script src="js/jquery-1.10.2.min.js"></script>
<title>Student Organization Home Page</title>

</head>
<body>
<form action="">
<div class='header'>Student Organization Home Page - Welcome <%=currentUsername%>!</div>
</form>

 <br>
 <div>Test for jsp vars <%=currentUsername%> PASS: <%=currentPassword%> STUDENTS: <%=students%></div>

<FORM METHOD="LINK" ACTION="jsp/students.jsp">
    <select>
        <option selected>Class 1</option>
        <option selected>Class 2</option>
        <option selected>Class 3</option>
        <option selected>Class 4</option>
    </select>
    <INPUT TYPE="submit" VALUE="Go To Students!">
    </FORM>
</body>

</html>


[ERROR   ] SRVE0777E: Exception thrown by application class 'com._jsp._homepage._jspService:98'
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
    at com._jsp._homepage._jspService(_homepage.java:98)
    at com.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:101)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at [internal classes]
    at com.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:215)
    at com.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1033)
    at [internal classes]
    at com.studentOrganizer.Login.doGet(Login.java:93)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
    at [internal classes]

[err] java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
[err]   at com._jsp._homepage._jspService(_homepage.java:98)
[err]   at com.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:101)
[err]   at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[err]   at [internal classes]
4

1 回答 1

6

我不擅长 JSP,但对我来说很清楚,你尝试students在 JSP 文件中显示单个字符串。但是students被定义为List。请在学生的 JSP 文件中循环运行。

就像是:

<%
ArrayList<String> students = teacherAccount .getStudents();
    Iterator<String> iterator = students .iterator();

    while (iterator.hasNext())
    {
      String student = (String)iterator.next();
      String pId = String.getId();
      ...
%>

假设您的问题在这里:

<div>Test for jsp vars <%=currentUsername%> 
PASS: <%=currentPassword%> STUDENTS: <%=students%></div>
                                           ^^^^     

改为forEach显示它

于 2013-08-15T18:20:00.643 回答