【发布时间】:2019-10-24 19:43:27
【问题描述】:
我在显示客户 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”。
注意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 类
包 de.java2enterprise.onlineshop.model;
导入 java.io.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")
公共类 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()) {
客户 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;
}
}
【问题讨论】:
-
看起来您没有一个名为
id的属性,而是一个名为ID的属性。
标签: java