在JSP页面中显示数据时获取java.lang.NumberFormatException

时间:2014-09-10 19:49:56

标签: java spring hibernate spring-mvc

我正在使用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 - 在第29行处理JSP页面/WEB-INF/views/studentlist.jsp时发生异常 根本原因 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;
}

控制器

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

2 个答案:

答案 0 :(得分:3)

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

${row.studentDetails} = List<StudentDetails>

JSTL正在尝试将FatherName转换为数字,以便它可以在List<StudentDetails>中获取该索引。

要解决此问题,您可以执行${row.studentDetails[0].fatherName},或者您可以在Student中添加一个名为getDefaultStudentData()的方法,该方法将返回第一个或#34; preffered&#34;学生详细信息对象然后引用它${row.defaultStudentData.fatherName}。或者当然,您可以根据学生☺

更改输出以处理多个学生详细信息

答案 1 :(得分:2)

在JSP中,变量row代表Student对象。因此,当您尝试访问此列表中的任何内容时,当您说row.studentDetails时,您正在获取java.util.List,那么JSP假定您正在尝试使用提供的索引获取列表中的元素。

所以它就像list.indexNumber,因此JSP假定FatherNameMotherName为数字,并尝试将它们转换为整数并获得异常。

要解决此问题,您必须遍历列表:

<c:forEach items="${srow.studentDetails}" var="details">
  ${details.FatherName}
  ${details.MotherName}
</c:forEach>

<强>更新

根据命名约定,您必须使用details.fatherNamedetails.motherName

访问属性

声明Java bean的标准方法是将属性命名为fatherName而不是FatherName

private String fatherName;
private String motherName;


public String getFatherName() {
    return fatherName;
}

public void setFatherName(String fatherName) {
    this.fatherName = fatherName;
}

public String getMotherName() {
    return motherName;
}

public void setMotherName(String motherName) {
    this.motherName = motherName;
}