gpt4 book ai didi

java - JSP页面显示数据时出现java.lang.NumberFormatException

转载 作者:行者123 更新时间:2023-12-01 17:05:29 24 4
gpt4 key购买 nike

我正在使用 Hibernate 和 Spring MVC。我试图从两个表中获取信息并将其显示在 jsp 页面中。这是我的表格:

表名称:学生

student_id    studentName
1. Jason Stathum

表名称:studentdetails

studentDetailsid   FatherName   MotherName    student_id
1 Mr.X Mrs. Y 1

在我的 JSP 页面中,我尝试显示如下数据:

 Sl#   Student              Father     Mother
1 Jason Stathum Mr.X Mrs.Y

当我运行应用程序以查看数据时,我收到以下错误消息:

HTTP 状态 500 - 处理 JSP 页面/WEB-INF/views/studentlist.jsp 第 29 行时发生异常根本原因java.lang.NumberFormatException:对于输入字符串:“FatherName” java.lang.NumberFormatException.forInputString(来源未知) java.lang.Integer.parseInt(来源未知) java.lang.Integer.parseInt(Unknown Source)...以及许多其他行...

我在下面包含了我的代码,你能告诉我我做错了什么吗?

非常感谢

实体:学生

@Entity
@Table(name = "student")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="student_id", nullable= false)
private Integer studentId;
private String studentName;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="student_id", referencedColumnName="student_id")
private List<StudentDetails> studentDetails = new ArrayList<StudentDetails>();

// Getters and Setters

public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}
public List<StudentDetails> getStudentDetails() {
return studentDetails;
}
public void setStudentDetails(List<StudentDetails> studentDetails) {
this.studentDetails = studentDetails;
}

实体:学生详细信息

@Entity
@Table(name = "studentDetails")
public class StudentDetails {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer studentDetailsId;
private String FatherName;
private String MotherName;

// Getters and Setters

public Integer getStudentDetailsId() {
return studentDetailsId;
}
public void setStudentDetailsId(Integer studentDetailsId) {
this.studentDetailsId = studentDetailsId;
}
public String getFatherName() {
return FatherName;
}
public void setFatherName(String fatherName) {
FatherName = fatherName;
}
public String getMotherName() {
return MotherName;
}
public void setMotherName(String motherName) {
MotherName = motherName;
}

Controller

@RequestMapping(value="studentlist", method = RequestMethod.GET)
public String getRecords(Model map)
{
List<Student> student = studentService.getAll();
map.addAttribute("student", student);
return "studentlist";
}

StudentDaoImpl

@Override
@SuppressWarnings("unchecked")
public List<Student> getAll() {
return getSessionFactory().openSession().createQuery("FROM Student").list();
}

JSP页面:学生列表

<c:if test="${!empty student}">
<table class="studentTable">
<tr>
<th>Sl#</th>
<th>Name</th>
<th>Father</th>
<th>Mother</th>

</tr>
<c:set var="count" value="0" scope="page" />
<c:forEach items="${student}" var="row">

<c:set var="count" value="${count + 1}" scope="page"/>
<tr>
<td>${count}</td>
<td>${row.studentName}</td>
<td>${row.studentDetails.FatherName}</td> <--- this is line 29
<td>${row.studentDetails.MotherName}</td>
</tr>
</c:forEach>
</table>
</c:if>

最佳答案

问题是您试图引用 List 的属性对象。

${row.studentDetails} =List<StudentDetails>

JSTL 正在尝试转换 FatherName到一个数字,这样它就可以得到 List<StudentDetails> 中的索引.

要解决这个问题,你可以这样做 ${row.studentDetails[0].fatherName}或者你可以在 Student 中放入一个方法称为类似 getDefaultStudentData()这将返回第一个或“首选”学生详细信息对象,然后像 ${row.defaultStudentData.fatherName} 一样引用它。或者当然您可以更改输出以处理每个学生的多个学生详细信息☺

关于java - JSP页面显示数据时出现java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25773898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com