0

I am using Hibernate and Spring MVC. I am trying to fetch information from two tables and display it in a jsp page . Here are my tables:

Table Name : student

student_id    studentName
  1.           Jason Stathum

Table Name : studentdetails

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

In my JSP page I am trying to display data like this:

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

When I run my application to see the data I get the following error message:

HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/views/studentlist.jsp at line 29 root cause java.lang.NumberFormatException: For input string: "FatherName" java.lang.NumberFormatException.forInputString(Unknown Source) java.lang.Integer.parseInt(Unknown Source) java.lang.Integer.parseInt(Unknown Source).... and many other lines...

I have included my codes below, Could you please tell me what I am doing wrong?

Thank you very much

Entity: Student

@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 : 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 Page: studentlist

<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>
1
  • Posting whole stacktrace might help in finding the root cause. Commented Sep 10, 2014 at 19:55

2 Answers 2

3

The problem is your trying to reference a property of an List object.

${row.studentDetails} = List<StudentDetails>

JSTL is trying to convert FatherName to a number so it can get that index in List<StudentDetails>.

To fix this you could do ${row.studentDetails[0].fatherName} or you could put a method in Student called something like getDefaultStudentData() that would return the first or "preffered" student details object then reference it like ${row.defaultStudentData.fatherName}. Or of course you could change your output to deal with multiple student details per Student ☺

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. I tried your code but I am getting this error message : javax.el.PropertyNotFoundException: Property 'FatherName' not found on type com.spring.org.model.StudentDetails javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)
@black_belt Use a lowercase to start with fatherName
Thanks a lot it worked. I tried this ${row.studentDetails[0].fatherName} and it worked. Could you please tell me why its working when I used lowercase?
The JSTL specifies that you use camel case that should always start with a lower case. Not just specific to this case but for all properties on a bean, its just how the language was written.
Thanks you very much. I am very new to Java , I have been trying to learn it all day and night.
1

In your JSP your variable row represents the Student object. So when you say row.studentDetails you are getting java.util.List now when you try to access anything on this list, then JSP assumes you are trying to fetch an element of the list using the provided index.

So it is like list.indexNumber, so JSP assumes FatherName and MotherName as number and tries to convert them to integer and gets the exception.

To fix this you have to iterate through the list:

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

Update:

As per naming convention you have to access the properties using details.fatherName and details.motherName

The standard way of declaring Java beans is to name the properties as fatherName instead of 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;
}

4 Comments

Thank you very much for your reply. I tried your code but this time when I ran my app I got this error: root cause javax.el.PropertyNotFoundException: Property 'FatherName' not found on type com.spring.org.model.StudentDetails javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)
If you have those properties defined in your StudentDetail object with proper getter and setter methods for the property then you should not get this exception, so verify once if everything are there.
I think my getter and setter methods are ok. I have updated my question above with the getter and setter codes. Do you think I might have problem with mapping the tables ? Thanks a lot
The naming convention you are following is not correct, the property name should be fatherName and motherName as per Jave Beans. Also even without doing any modifications to your Java code you can access them as you have getter and setter methods with proper naming convention. I have updated my answer accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.