日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[java] javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student

發布時間:2024/5/28 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [java] javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題提出:

在使用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}" οnclick="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方法:

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/
? ? ? ??


總結

以上是生活随笔為你收集整理的[java] javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。