我正在做一个项目,搜索我的数据库中的特定成员,搜索他的 memberId ,然后显示所选信息,但我的Java类Setter出现问题(不确定是否真的出问题)因为我一直收到来自catch的错误消息(Exception ex)
javax.el.PropertyNotFoundException:类'Project.Members'没有属性'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 类:
公共班级成员{
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 类吗?我真的很无能为力了...请帮忙......
答案 0 :(得分:3)
您应该尊重Java命名约定。变量始终以小写字母开头。属性也以小写字母开头。由于会员级代表一个会员,您也应该将其称为会员,而不是会员。
request.setAttribute("member", std);
public class Member {
private String firstName;
// ...
public String getFirstName() {
return this.firstName;
}
}
Id: ${member.memberId}
Name: ${member.firstName}<br/>