不断收到 javax.el.PropertyNotFoundException:类“...”没有属性“...”

发布于 2024-10-21 07:20:33 字数 4579 浏览 3 评论 0原文

我正在做一个项目,该项目从我的数据库中搜索特定成员,搜索他的 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}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-10-28 07:20:33

您应该尊重 Java 命名约定。变量始终以小写字母开头。属性也以小写字母开头。由于 Members 类代表一个成员,因此您也应该将其称为 Member,而不是 Members。

request.setAttribute("member", std);

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

Id: ${member.memberId}     
Name: ${member.firstName}<br/>

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.

request.setAttribute("member", std);

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

Id: ${member.memberId}     
Name: ${member.firstName}<br/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文