簡體   English   中英

HTTP狀態500沒有屬性“id”

[英]HTTP Status 500 does not have the property 'id'

當我顯示客戶的ID時,我收到錯誤消息。 我也嘗試更改屬性'id'的名稱,但是新名稱出現了同樣的錯誤。

HTTP狀態500 - 內部服務器錯誤

類型異常報告消息內部服務器錯誤說明服務器遇到內部錯誤,導致無法完成此請求。

異常 org.apache.jasper.JasperException:javax.el.PropertyNotFoundException:類'de.java2enterprise.onlineshop.model.Customer'沒有屬性'id'。

根本原因 javax.el.PropertyNotFoundException:類'de.java2enterprise.onlineshop.model.Customer'沒有屬性'id'。

note備注 GlassFish Server Open Source Edition 5.1.0日志中提供了異常的完整堆棧跟蹤及其根本原因。

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類

package de.java2enterprise.onlineshop.model;

import java.io.Serializable;

public class Customer實現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() + 
        "]";
}

}

調節器

@WebServlet("/userShow")

public class UserServlet擴展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()) {

客戶客戶=新客戶();

            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;
}

}

您的Customer類需要遵循Java bean的命名約定:

它是getId/setId而不是getID/setID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM