嗨,我在过去的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]

最佳答案

我对JSP的了解不强,但是我很清楚,您尝试在JSP文件中显示students像单个String一样。但是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代替显示

关于java - 尝试为类的实例设置成员变量时,Java ArrayList类转换异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18258956/

10-12 04:53