[java] javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student,springbeanproperty
問題提出:
在使用MyEclipse開發Java Web時,調用DAO和Java Bean出現了如下錯誤:
嚴重: Servlet.service() for servlet [jsp] in context with path [/JDBCbyDao] threw exception [An exception occurred processing JSP page /student.jsp at line 37
34:
35: <c:forEach items="${ studentList }" var="student">
36: <tr bgcolor="#FFFFFF">
37: <td><input type="checkbox" name="id" value="${ student.id }" /></td>
38: <td>${ student.id }</td>
39: <td>${ student.name }</td>
40: <td>${ student.password }</td>
Stacktrace:] with root cause
javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:290)
at javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
其中我的類中已經定義了屬性和get/set方法,如下:
package bean;public class Student {private Integer id; //學號private String name; //姓名private String password; //密碼public Integer getID() { return id; }public String getName() { return name; }public String getPassword() { return password; }public void setID(Integer id) { this.id = id; }public void setName(String name) { this.name = name; }public void setPassword(String pwd) { this.password = pwd; }}而Jsp中的調用代碼是通過EL實現,也匯入了相應的包。如下:
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><jsp:directive.page import="DAO.StudentDAO"/><jsp:directive.page import="java.util.List"/><%List studentList = StudentDAO.listStudents();request.setAttribute("studentList", studentList);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'student.jsp' starting page</title> </head> <body> <form action="operateStudent.jsp" method=get><table bgcolor="#CCCCCC" cellspacing=1 cellpadding=5 width=100%><tr bgcolor=#DDDDDD><th>選擇</th><th>學號</th><th>姓名</th><th>密碼</th><th>操作</th></tr><c:forEach items="${studentList}" var="stu"><tr bgcolor="#FFFFFF"><td><input type="checkbox" name="id" value="${stu.id}" /></td><td>${stu.id}</td><td>${stu.name}</td><td>${stu.password}</td><td><a href="addEmployee.jsp?action=edit&id=${stu.id}">修改</a><a href="addEmployee.jsp?action=del&id=${stu.id}" onclick="return confirm('確定刪除?')">刪除</a></td></tr></c:forEach></table></form> </body></html>
解決方案:
1.可能你遇到的錯誤是“Property 'id' not found on type java.lang.String”異常
它的意思是String類中沒有id這個屬性,而修改的方法就是:
<c:forEach items="videos" var="video" >
修改成:
<c:forEach items="${videos}" var="video" >
但是你需要注意它的錯誤是: java.lang.String對比type bean.Student,其中對應src/bean.Student.java檔案。而且我在JSP中已經是${studentList}這種變數了,所以該方法不是該錯誤的解決方案。
2.有人說是bean的屬性名稱錯誤,或者沒有get,set方法,但是我的bean如下方法。又參考錯誤“javax.el.PropertyNotFoundException: Property 'pNum' not found on type com.manager.Paper”,此時的解決方案是:
private int pNum;
private int pSize;
建議你將這兩個屬性的名稱換下
private int pnum;
private int psize;
據說是應為命名規範,同時stu.EmpNo估計是大小寫錯了,換成 ${stu.empNo} 就能成功,因為EL是讀取屬性的getter方法的,一般按照屬性首字母小寫來處理。但是我的名字是id,因此該方法也是行不通的。
3.如果上面兩個方法你仍然報錯,下面是我自己總結的方法:
Servlet.service() for servlet [jsp] in context with path
javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student
你需要做到的是:
(1).首先確保迴圈<c:forEach items="${studentList}" var="stu">,然後調用是${stu.id}、${stu.name};
(2).然後屬性命名最好是小寫,當然首字母一定要小寫,如empNo;
(3).在資料庫中create table student( stuid int,username varchar(20) )對應的Student類變數private Integer id; private String name;其中類型需要一致,同時設定get和set方法:
<span style="white-space:pre"></span>private Integer id; //學號private String name; //姓名 public Integer getID() { return id; } public String getName() { return name; } public void setId(Integer id) { this.id = id; }public void setName(String name) { this.name = name; }(4).在DAO中資料庫增刪改查操作中類型要一致,並且對應資料庫中的學號stuid和姓名username:
//刪除操作
public static int delete(Integer id) throws Exception {
String sql = "DELETE FROM student WHEREstuid = ? ";
return JDBCConnect.executeUpdate(sql, id);
}
//查詢操作
student.setId(rs.getInt("stuid"));
student.setName(rs.getString("username"));
(5).如果上面的資料庫、Java類變數類型都是一致的,使用方法都正確仍然存在該錯誤,那可能就是下面的錯誤:
當我定義函數public Integer getID() { return id; }時就會報錯
HTTP Status 500 - javax.el.PropertyNotFoundException: Property 'id' not readable on type bean.Student
而當我修改為public IntegergetId() { return id; }後運行結果如所示:
同樣setId()方法也修改,同時DAO中調用setId()和getId()方法也修改“D=>d”。所以我還是懷疑是使用EL時的命名規範在作怪。而且我通過代碼驗證修改成小寫d可以成功。而第一個錯誤Servlet.service() for servlet需要看它後面拋出的異常,即第二個錯誤。
另一種猜測:在jstl的el運算式引用錯誤應該使用${info.type.id } 而不是${info.id }(未驗證)
參考資料:
1.JSP not finding property in bean - stackoverflow 丟失set方法
2.javax.el.PropertyNotFoundException: Property 'answer' not - stackoverflow
3.異常:javax.el.PropertyNotFoundException: Property 'id' not found on - CSDN
4.javax.el.PropertyNotFoundException: Property 'Owner' not found on - 百度知道
5.JSP沒有使<c:forEach items="${specialty}" var="spe"> - CSDN論壇
該錯誤報表和線上筆記希望對你有所協助~
(By:Eastmount 2015-5-19 淩晨4點 http://blog.csdn.net/eastmount/)