我试图在表格中显示所有员工列表,并在同一个表格中删除它。但这个错误一直困扰着我。可能是什么原因?
May 28, 2017 9:25:27 PM
org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
javax.el.PropertyNotFoundException: Property 'EmployeeID' not found on type bean.Employee
May 28, 2017 9:25:27 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DisplayAllEmp] in context with path [/ELMS] threw exception [An exception occurred processing JSP page /elms/elms_skms/DeleteEmployeeAcc.jsp at line 55
52: <tbody>
53: <c:forEach items="${employee}" var="emp">
54: <tr>
55: <td><c:out value="${emp.EmployeeID}" /></td>
56: <td><c:out value="${emp.EmployeeIC}" /></td>
57: <td><c:out value="${emp.EmployeeName}" /></td>
58: <td><c:out value="${emp.EmployeePosition}" /></td>
Stacktrace:] with root cause
javax.el.PropertyNotFoundException: Property 'EmployeeID' not found on type bean.Employee
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:266)
at javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
这是我的jsp页面:
<div class="module-body table">
<form name = delete" method="POST" action="DeleteEmployeeServlet">
<table cellpadding="0" cellspacing="0" border="0" class="datatable-1 table
table-bordered table-striped display" width="50%">
<thead>
<tr>
<th>Employee ID</th>
<th>Employee IC</th>
<th>Employee Name</th>
<th>Employee Position</th>
<th>Employee Session</th>
<th>Employee Grade</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${employee}" var="emp">
<tr>
<td><c:out value="${emp.EmployeeID}" /></td>
<td><c:out value="${emp.EmployeeIC}" /></td>
<td><c:out value="${emp.EmployeeName}" /></td>
<td><c:out value="${emp.EmployeePosition}" /></td>
<td><c:out value="${emp.EmployeeSession}" /></td>
<td><c:out value="${emp.EmployeeGrade}" /></td>
<td><a href="DeleteEmployeeServlet?table=employee&employeeid=<c:out
value="${emp.EmployeeID}" />"onclick="return confirm('Are you
sure?');"><i class="icon-trash"></i></a> <br></td>
</c:forEach>
</tbody>
这是我的servlet页面:
package servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.EmployeeDAO;
import bean.Employee;
public class DeleteEmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private EmployeeDAO dao;
public DeleteEmployeeServlet() {
super();
dao = new EmployeeDAO();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String forward = "";
String table = request.getParameter("table");
if(table.equalsIgnoreCase("employee")){
int id = Integer.parseInt(request.getParameter("EmployeeID"));
dao.DeleteEmployeeDetails(id);
forward = "DisplayAllEmp?table=employee";
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
这是我的模型/ DAO(viewEmployee):
public List<Employee> viewEmployee() {
List<Employee> employee = new ArrayList<Employee>();
try {
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
ResultSet rs = stmt.executeQuery("select * from employee");
while (rs.next()) {
Employee e = new Employee();
e.setEmployeeID(rs.getInt("EmployeeID"));
e.setEmployeePassword(rs.getString("EmployeePassword"));
e.setEmployeeName(rs.getString("EmployeeName"));
e.setEmployeeIC(rs.getString("EmployeeIC"));
e.setEmployeeGender(rs.getString("EmployeeGender"));
e.setEmployeeEmail(rs.getString("EmployeeEmail"));
e.setEmployeePhoneNo(rs.getString("EmployeePhoneNo"));
e.setEmployeeStreet1(rs.getString("EmployeeStreet1"));
e.setEmployeeStreet2(rs.getString("EmployeeStreet2"));
e.setEmployeePCode(rs.getInt("EmployeePCode"));
e.setEmployeeCity(rs.getString("EmployeeCity"));
e.setEmployeeState(rs.getString("EmployeeState"));
e.setEmployeeQualification(rs.getString("EmployeeQualification"));
e.setEmployeePosition(rs.getString("EmployeePosition"));
e.setEmployeeSession(rs.getString("EmployeeSession"));
e.setEmployeeGrade(rs.getInt("EmployeeGrade"));
e.setEmploymentDate(rs.getDate("EmploymentDate"));
employee.add(e);
System.out.println("Employee id: " + e.getEmployeeID());
}
System.out.println("All rows from table view Employee retrieved!");
} catch (SQLException e) {
System.out.println("Failed: An SQL Exception occured : " + e);
e.printStackTrace();
}
return employee;
}
这是我的DisplayAllEmp的DAO:
public void DeleteEmployeeDetails(int id) {
try {
currentCon = ConnectionManager.getConnection();
ps = currentCon.prepareStatement("delete from employee where employeeID=?");
// Parameters start with 1
ps.setInt(1, id);
ps.executeUpdate();
System.out.println("1 row of table Employee has been deleted. employeeID:" + id);
} catch (SQLException e) {
e.printStackTrace();
}
}
这是我的bean(员工):
package bean;
import java.util.Date;
public class Employee {
private int EmployeeID;
private String EmployeePassword;
private String EmployeeName;
private String EmployeeIC;
private String EmployeeGender;
private String EmployeeEmail;
private String EmployeePhoneNo;
private String EmployeeStreet1;
private String EmployeeStreet2;
private int EmployeePCode;
private String EmployeeCity;
private String EmployeeState;
private String EmployeeQualification;
private String EmployeePosition;
private String EmployeeSession;
private int EmployeeGrade;
private Date EmploymentDate;
private boolean valid;
public Employee() {
EmployeeID = 0;
EmployeePassword = null;
EmployeeName=null;
EmployeeIC=null;
EmployeeGender=null;
EmployeeEmail=null;
EmployeePhoneNo=null;
EmployeeStreet1=null;
EmployeeStreet2=null;
EmployeePCode=0;
EmployeeCity=null;
EmployeeState=null;
EmployeeQualification=null;
EmployeePosition=null;
EmployeeSession=null;
EmployeeGrade=0;
EmploymentDate=null;
}
public Employee(int employeeID, String employeePassword, String employeeName, String employeeIC, String employeeGender, String employeeEmail, String employeePhone, String employeeStreet1, String employeeStreet2, int employeePCode, String employeeCity, String employeeState, String employeeQuaification, String employeePosition, String employeeSession, int employeeGrade, Date employmentDate) {
EmployeeID = employeeID;
EmployeePassword = employeePassword;
EmployeeName=employeeName;
EmployeeIC=employeeIC;
EmployeeGender=employeeGender;
EmployeeEmail=employeeEmail;
EmployeePhoneNo=employeePhone;
EmployeeStreet1=employeeStreet1;
EmployeeStreet2=employeeStreet2;
EmployeePCode=employeePCode;
EmployeeCity=employeeCity;
EmployeeState=employeeState;
EmployeeQualification=employeeQuaification;
EmployeePosition=employeePosition;
EmployeeSession=employeeSession;
EmployeeGrade=employeeGrade;
EmploymentDate=employmentDate;
}
public int getEmployeeID() {return EmployeeID;}
public void setEmployeeID(int employeeID) {EmployeeID = employeeID;}
public String getEmployeePassword() {return EmployeePassword;}
public void setEmployeePassword(String employeePassword) {EmployeePassword = employeePassword;}
public String getEmployeeName() {return EmployeeName;}
public void setEmployeeName(String employeeName) {EmployeeName = employeeName;}
public String getEmployeeIC() {return EmployeeIC;}
public void setEmployeeIC(String employeeIC) {EmployeeIC = employeeIC;}
public String getEmployeeGender() {return EmployeeGender;}
public void setEmployeeGender(String employeeGender) {EmployeeGender = employeeGender;}
public String getEmployeeEmail() {return EmployeeEmail;}
public void setEmployeeEmail(String employeeEmail) {EmployeeEmail = employeeEmail;}
public String getEmployeePhoneNo() {return EmployeePhoneNo;}
public void setEmployeePhoneNo(String employeePhoneNo) {EmployeePhoneNo = employeePhoneNo;}
public String getEmployeeStreet1() {return EmployeeStreet1;}
public void setEmployeeStreet1(String employeeStreet1) {EmployeeStreet1 = employeeStreet1;}
public String getEmployeeStreet2() {return EmployeeStreet2;}
public void setEmployeeStreet2(String employeeStreet2) {EmployeeStreet2 = employeeStreet2;}
public int getEmployeePCode() {return EmployeePCode;}
public void setEmployeePCode(int employeePCode) {EmployeePCode = employeePCode;}
public String getEmployeeCity() {return EmployeeCity;}
public void setEmployeeCity(String employeeCity) {EmployeeCity = employeeCity;}
public String getEmployeeState() {return EmployeeState;}
public void setEmployeeState(String employeeState) {EmployeeState = employeeState;}
public String getEmployeeQualification() {return EmployeeQualification;}
public void setEmployeeQualification(String employeeQualification) {EmployeeQualification = employeeQualification;}
public String getEmployeePosition() {return EmployeePosition;}
public void setEmployeePosition(String employeePosition) {EmployeePosition = employeePosition;}
public String getEmployeeSession() {return EmployeeSession;}
public void setEmployeeSession(String employeeSession) {EmployeeSession = employeeSession;}
public int getEmployeeGrade() {return EmployeeGrade;}
public void setEmployeeGrade(int employeeGrade) {EmployeeGrade = employeeGrade;}
public Date getEmploymentDate() {return EmploymentDate;}
public void setEmploymentDate(Date employmentDate) {EmploymentDate = employmentDate;}
public boolean isValid() {return valid;}
public void setValid(boolean valid) {this.valid = valid;}
}
答案 0 :(得分:0)
您的Employee
类只有私有字段,因此您无法使用点表示法访问它们。 setters
和getters
是公开的,可在课堂外访问。使用它们可以在jsp
文件中检索所需的信息。
此外,请查看Oracle的文档页面Declaring Member Variables,Access Modifiers
部分。
公共修饰符 - 可以从所有类访问该字段。
私有修饰符 - 该字段只能在自己的类中访问。
当您撰写${emp.EmployeeID}
时,您要求的是名为EmployeeID
的字段,其中包含public
访问修饰符,该字段不存在。