2014-09-10 28 views
0

我正在使用Hibernate和Spring MVC。我試圖從兩個表中獲取信息並將其顯示在一個jsp頁面中。這裏是我的表:在JSP頁面中顯示數據時獲取java.lang.NumberFormatException

表名:學生

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(Unknown Source) java.lang.Integer.parseInt(Unknown Source) 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; 
} 

實體: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頁面: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> 
+0

發佈整個堆棧跟蹤可能有助於找到根本原因。 – 2014-09-10 19:55:05

回答

3

問題是您試圖引用List對象的屬性。

${row.studentDetails} = List<StudentDetails>

JSTL試圖FatherName轉換爲數字,因此可以得到指數List<StudentDetails>

爲了解決這個問題,你可以做${row.studentDetails[0].fatherName}或者你可以把一個方法Student稱爲像getDefaultStudentData(),將返回第一或再「參訪」學生信息對象引用它像${row.defaultStudentData.fatherName}。或者當然你可以改變你的輸出來處理每個學生的多個學生細節☺

+0

感謝您的回覆。我嘗試了你的代碼,但是我收到了這個錯誤信息:'javax.el.PropertyNotFoundException:Property'FatherName'not found on type com.spring.org.model.StudentDetails \t javax.el.BeanELResolver $ BeanProperties.get(BeanELResolver。 java:237)' – 2014-09-10 20:08:35

+1

@black_belt使用小寫字母以'fatherName'開頭 – 2014-09-10 20:22:26

+0

非常感謝它的工作。我試過這個'$ {row.studentDetails [0] .fatherName}',它工作。您能否告訴我爲什麼使用小寫字母時它的工作原理? – 2014-09-10 20:25:14

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; 
} 
+0

非常感謝您的回覆。我嘗試了你的代碼,但是這次我運行我的應用程序時出現了這個錯誤:'根本原因 javax.el.PropertyNotFoundException:在類型com.spring.org.model.StudentDetails上未找到屬性'FatherName' \t javax.el .BeanELResolver $ BeanProperties.get(BeanELResolver.java:237)' – 2014-09-10 20:04:32

+0

如果你在'StudentDetail'對象中定義了那些屬性,並且該屬性有適當的getter和setter方法,那麼你不應該得到這個異常,所以如果一切都是那裏。 – Chaitanya 2014-09-10 20:12:59

+0

我認爲我的getter和setter方法都可以。我用getter和setter代碼更新了我的問題。你認爲我可能在映射表時遇到問題嗎?非常感謝 – 2014-09-10 20:17:13

相關問題