我有一个employeeDB类,我在其中设置了与db的连接,我可以从db中读取数据。在这个类中,我有一个名为getAllEmaployee
的函数,用于检索数据,它看起来像这样:
public class EmployeeDB {
private Connection connection;
public EmployeeDB() {
connection = DBUtil.getConnection();
}
public HashMap<String, List<String>> getAllEmployee() throws FileNotFoundException, UnsupportedEncodingException {
HashMap<String, List<String>> total = new HashMap<>();
try {
Statement statementE = connection.createStatement();
PreparedStatement pStatement = connection.prepareStatement("select month from paycheck where id=?");
ResultSet rse = statementE.executeQuery("select id from employee");
while (rse.next()) {
List<String> months = new ArrayList<>();
pStatement.setString(1, rse.getString("id"));
ResultSet rs = pStatement.executeQuery();
while (rs.next()) {
months.add(rs.getString("month"));
}
total.put(rse.getString("id"), months);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
}
现在,我从我的servlet调用这个方法&#34; EmployeeInfo&#34;并返回具有我需要的所有数据对的hashmap(我已经打印过,看到我有正确的数据)。
现在我希望从servlet将此hashmap设置为会话的属性,然后转到我的jsp文件并使用此hashmap数据填充表。这是我的servlet:
public class EmployeeInfo extends HttpServlet {
private final EmployeeDB edb;
public EmployeeInfo() {
super();
edb = new EmployeeDB();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String forward = "";
Employee e = new Employee();
Employee retEmp = new Employee();
HashMap<String, List<String>> total = new HashMap<>();
e.setId(request.getParameter("ID"));
try {
retEmp = edb.getEmpInfo(e.getId());
if (e.getId().equals("")) {
total=edb.getAllEmployee();
request.getSession().setAttribute("EmployeeHashMap", total);
forward = "AllEmployee.jsp?";
}
else if (retEmp.getId() == null) {
forward = "errorID.jsp?";
} else {
request.getSession().setAttribute("newEmployee", retEmp);
forward = "WelcomeUser.jsp";
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(EmployeeInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
现在在AllEmployee.jsp
我希望填充表格,它看起来像这样:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table id="ptable" border="1">
<tr>
<td style="text-align: center;">ID</td>
<td style="text-align: center;">Month</td>
</tr>
<c:forEach var="entry" items="${EmployeeHashMap}" >
<!-- entry.key is employee.key -->
<!-- entry.value is employee.skills -->
<c:forEach var="skillId" items="${entry.value}" >
<tr>
<td>${entry.key}</td>
<td>${skillId}</td>
</tr>
</c:forEach>
</c:forEach>
</table>
</body>
</html>Type a message
我不知道为什么它不工作,我可以看到一张空桌子...... 请尽量帮我解决这个问题。感谢!!!