我一直在尝试通过ArrayList在Servlet的帮助下在JSP中打印ResultSet。JDBC部分似乎没问题.ArrayList可以在JSP中获取并且可以打印。但是当我想要迭代时列表传递给带有<c:forEach>
标记的JSP,它说的是我的bean类&#39; PureJavaClasses.employeeClass&#39;没有属性&#39; Id&#39;。我已经尝试过在SO上发布了一系列不同的解决方案,但都是徒劳的。我不知道该怎么做。我是JavaEE的初学者。
这是我的Bean类:
public class employeeClass implements java.io.Serializable {
private int id;
private String name;
private String job;
private int subjectId;
private int salary;
public employeeClass() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSubjectid() {
return subjectId;
}
public void setSubjectid(int subjectId) {
this.subjectId = subjectId;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "id=" + id +
", name=" + name +
", job=" + job +
", subjectId=" + subjectId +
", salary=" + salary;
}
}
这是servlet:
private void show_table(HttpServletRequest request,HttpServletResponse response){
Connection connection = ServletLogin.connection;
String query = "SELECT * FROM `dog`.`employee`";
ArrayList<employeeClass> list = new ArrayList<>();
try {
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
employeeClass obj2 = new employeeClass();
obj2.setId(rs.getInt("ID"));
obj2.setName(rs.getString("Name"));
obj2.setJob(rs.getString("Job_Designation"));
obj2.setSubjectid(rs.getInt("Subject ID"));
obj2.setSalary(rs.getInt("Salary"));
list.add(obj2);
}
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("listEmp",list);
try {
request.getRequestDispatcher("/Pages/EmployeeTable.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我的EmployeeTable.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.ArrayList"%>
<html>
<head>
<title>Employee List</title>
</head>
<body>
<% ArrayList list1 =(ArrayList) request.getAttribute("listEmp"); %>
<%=list1%>
<table>
<th>ID</th>
<th>Name</th>
<th>Job Designation</th>
<th>Subject ID</th>
<th>Salary</th>
<c:forEach items="${listEmp}" var="list">
<tr>
<td><c:out value="${list.Id}"/></td>
<td><c:out value="${list.name}"/></td>
<td><c:out value="${list.job}"/></td>
<td><c:out value="${list.subjectId}"/></td>
<td><c:out value="${list.salary}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>ServletLogin</servlet-name>
<servlet-class>ServletLogin</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletEmployee</servlet-name>
<servlet-class>ServletEmployee</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLogin</servlet-name>
<url-pattern>/ServletLogin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletEmployee</servlet-name>
<url-pattern>/ServletEmployee</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
答案 0 :(得分:1)
我认为您需要通过提供正确的名称来解决问题。就像使用id而不是Id等那样应该解决它。
<td><c:out value="${list.id}"/></td>