简体   繁体   中英

HTTP Status 500 does not have the property 'id'

I get Error Messsage when I display the ID of the customer. I have too try to change the name of the property 'id' but the came the same error with new name.

HTTP Status 500 - Internal Server Error

type Exception report message Internal Server Error description The server encountered an internal error that prevented it from fulfilling this request.

exception org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: The class 'de.java2enterprise.onlineshop.model.Customer' does not have the property 'id'.

root cause javax.el.PropertyNotFoundException: The class 'de.java2enterprise.onlineshop.model.Customer' does not have the property 'id'.

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 5.1.0 logs.

HTML:

 <%@ include file="header.jspf" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Onlineshop</title> </head> <body> <c:forEach var="customer" items="${customers}"> <article> <p>${customer.id}</p> <p>${customer.email}</p> <p>test</p> </article> </c:forEach> <%@ include file="footer.jspf" %> 

Java Class

package de.java2enterprise.onlineshop.model;

import java.io.Serializable;

public class Customer implements Serializable{ private static final long serialVersionUID = 1L;

private Long id;
private String email;
private String password;

public Customer() {}

public Customer(String email, String password) {
    this.email = email;
    this.password = password;
}

public Long getID() {
    return id;
}

public void setID(Long id) {
    this.id = id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String toString() {
    return 
        "[" +
        getID() + ", " +
        getEmail() + ", " +
        getPassword() + 
        "]";
}

}

Controller

@WebServlet("/userShow")

public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L;

@Resource
private DataSource ds;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}
public void doPost( HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {

    try {
        List<Customer> customers = find();
        if (customers != null) {
            HttpSession session = request.getSession();
            session.setAttribute("customers", customers);
        }
    } catch (Exception e) {
        throw new ServletException(e.getMessage());
    }
    RequestDispatcher dispatcher = request.getRequestDispatcher("search.jsp");
    dispatcher.forward(request, response);
}

public List<Customer> find() throws Exception {

    List<Customer> customers = new ArrayList<Customer>();
    try (final Connection con = ds.getConnection();
            final PreparedStatement stmt = con
                    .prepareStatement(
                            "SELECT " +
                                    "id, " +
                                    "email, " +
                                    "password " +
                                    "FROM onlineshop.customer ")) {
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {

Customer customer = new Customer();

            Long id = Long.valueOf(rs.getLong("id"));
            customer.setID(id);

            String email = rs.getString("email");
            customer.setEmail(email);

            String password = rs.getString("password");
            customer.setPassword(password);
            customers.add(customer);

        }
    }
    return customers;
}

}

Your Customer class needs to follow the naming conventions for Java beans:

It's getId/setId instead of getID/setID .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM