不断收到 javax.el.PropertyNotFoundException:类“...”没有属性“...”
我正在做一个项目,该项目从我的数据库中搜索特定成员,搜索他的 memberId,然后显示选定的信息,但是我的 Java 类设置器遇到问题(不是真的确定这是否真的是问题)因为我不断收到来自 catch(Exception ex)
javax.el.PropertyNotFoundException: The class 'Project.Members' does not have the property 'FirstName' 的错误消息。
你能帮我找出我的代码出了什么问题吗?
这是我的 servlet 代码:
public class SearchAMember extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String query = "SELECT memberId, FirstName FROM members WHERE memberId = ?";
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SelectDemo</title>");
out.println("</head>");
out.println("<body>");
try {
Class.forName(Connect.DRIVER);
con = DriverManager.getConnection(Connect.CONNECTION_STRING,
Connect.USERNAME, Connect.PASSWORD);
stmt = con.prepareStatement(query);
int findId = Integer.parseInt(request.getParameter("id"));
stmt.setInt(1, findId);
rs = stmt.executeQuery();
Members std;
if (rs.next()) {
std = new Members();
std.setMemberId(rs.getInt("memberId"));
std.setFirstName(rs.getString("FirstName"));
request.setAttribute("Members", std);
RequestDispatcher rd = request.getRequestDispatcher("/DisplayAllMembers.jsp");
rd.forward(request, response);
} else {
out.println("No record found...");
}
} catch (ClassNotFoundException ex) {
out.println("Driver not found...");
} catch (SQLException ex) {
out.println("Failed connection or query...." + ex.getMessage());
} catch (Exception ex) {
out.println("Error.... " + ex.getMessage());
} finally {
try {
con.close();
} catch (SQLException ex) {
}
}
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
/*<--HttpServlet Method was here-->*/
}
}
和我的 getter 和 setter 的 Member.java 类:
public class Members {
private int memberId;
private String Username;
private String Password;
private String FirstName;
private String MiddleName;
private String LastName;
private String Address;
private String Email;
private int CurrPoints;
public String getAddress() {
return Address;
}
public int getCurrPoints() {
return CurrPoints;
}
public String getEmail() {
return Email;
}
public String getFirstName() {
return FirstName;
}
public String getLastName() {
return LastName;
}
public String getMiddleName() {
return MiddleName;
}
public String getPassword() {
return Password;
}
public String getUsername() {
return Username;
}
public int getMemberId() {
return memberId;
}
public void setAddress(String Address) {
this.Address = Address;
}
public void setCurrPoints(int CurrPoints) {
this.CurrPoints = CurrPoints;
}
public void setEmail(String Email) {
this.Email = Email;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public void setMiddleName(String MiddleName) {
this.MiddleName = MiddleName;
}
public void setPassword(String Password) {
this.Password = Password;
}
public void setUsername(String Username) {
this.Username = Username;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
}
这也是我的 Jsp :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Members Information</h1>
Id: ${Members.memberId}
Name: ${Members.FirstName}<br/>
</body>
问题是否出在我的 Members.java 类上?我真的不知道出了什么问题......请帮忙......
I am doing a project which searches a specific member from my database searching for his memberId and then show selected information, but I'm having trouble with my Java Class Setter(not really sure if it was really the problem) because I keep on receiving error message from the catch(Exception ex)
javax.el.PropertyNotFoundException: The class 'Project.Members' does not have the property 'FirstName'.
Can you help me figure out what was wrong with my code?
This is my servlet code:
public class SearchAMember extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String query = "SELECT memberId, FirstName FROM members WHERE memberId = ?";
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SelectDemo</title>");
out.println("</head>");
out.println("<body>");
try {
Class.forName(Connect.DRIVER);
con = DriverManager.getConnection(Connect.CONNECTION_STRING,
Connect.USERNAME, Connect.PASSWORD);
stmt = con.prepareStatement(query);
int findId = Integer.parseInt(request.getParameter("id"));
stmt.setInt(1, findId);
rs = stmt.executeQuery();
Members std;
if (rs.next()) {
std = new Members();
std.setMemberId(rs.getInt("memberId"));
std.setFirstName(rs.getString("FirstName"));
request.setAttribute("Members", std);
RequestDispatcher rd = request.getRequestDispatcher("/DisplayAllMembers.jsp");
rd.forward(request, response);
} else {
out.println("No record found...");
}
} catch (ClassNotFoundException ex) {
out.println("Driver not found...");
} catch (SQLException ex) {
out.println("Failed connection or query...." + ex.getMessage());
} catch (Exception ex) {
out.println("Error.... " + ex.getMessage());
} finally {
try {
con.close();
} catch (SQLException ex) {
}
}
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
/*<--HttpServlet Method was here-->*/
}
}
and my Member.java class for my getter and setter:
public class Members {
private int memberId;
private String Username;
private String Password;
private String FirstName;
private String MiddleName;
private String LastName;
private String Address;
private String Email;
private int CurrPoints;
public String getAddress() {
return Address;
}
public int getCurrPoints() {
return CurrPoints;
}
public String getEmail() {
return Email;
}
public String getFirstName() {
return FirstName;
}
public String getLastName() {
return LastName;
}
public String getMiddleName() {
return MiddleName;
}
public String getPassword() {
return Password;
}
public String getUsername() {
return Username;
}
public int getMemberId() {
return memberId;
}
public void setAddress(String Address) {
this.Address = Address;
}
public void setCurrPoints(int CurrPoints) {
this.CurrPoints = CurrPoints;
}
public void setEmail(String Email) {
this.Email = Email;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public void setMiddleName(String MiddleName) {
this.MiddleName = MiddleName;
}
public void setPassword(String Password) {
this.Password = Password;
}
public void setUsername(String Username) {
this.Username = Username;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
}
also this is my Jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Members Information</h1>
Id: ${Members.memberId}
Name: ${Members.FirstName}<br/>
</body>
Does the problem was at my Members.java class? I am really clueless what was wrong with it... please help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该尊重 Java 命名约定。变量始终以小写字母开头。属性也以小写字母开头。由于 Members 类代表一个成员,因此您也应该将其称为 Member,而不是 Members。
You should respect the Java naming conventions. Variables always start with a lowercase letter. Properties also start with a lowercase letter. And since the Members class represents one member, you should also call it Member, not Members.