2011-03-12 60 views
1

我在做一個項目,它從我的數據庫中搜索他的memberId然後顯示選定的信息,但我和我的Java類二傳手麻煩(真的不知道,如果它是真的這個問題),因爲我一直在趕上(異常前)收到錯誤消息繼續接收javax.el.PropertyNotFoundException:類'...'沒有屬性'...'

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-->*/ 
} 

}

和我Member.java類我的getter和setter:

公共類成員{

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}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    Name: ${Members.FirstName}<br/> 
</body> 

請問問題出在我的Members.java類?我真的很無知,它有什麼問題...請幫助...

回答

3

你應該尊重Java命名約定。變量始終以小寫字母開頭。屬性也以小寫字母開頭。既然會員類代表一個成員,你也應該稱之爲成員,而不是成員。

request.setAttribute("member", std); 

public class Member { 
    private String firstName; 
    // ... 
    public String getFirstName() { 
     return this.firstName; 
    } 
} 

Id: ${member.memberId}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Name: ${member.firstName}<br/> 
+0

非常感謝你先生JB Nizet ...我忘了Java的做和不要現在我已經吸取了教訓...再次感謝主席:) – iamanapprentice 2011-03-13 06:05:09

相關問題