Spring MVC将多个列表从控制器传递到jsp

时间:2012-12-10 06:01:57

标签: java jsp spring-mvc jstl

我有两个对象EmployeeDepartment员工包含dept_id。我想要做的是在jsp页面的表格中显示Employee的内容。但是,我希望在dept_id表中显示dept_name,而不是显示Department。到目前为止,我的控制器方法为:

public ModelAndView viewEmployee(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        List<Employee> employeeList = employeeService.getAllEmployee();
        List<Department> departmentList = new ArrayList<Department>();
        for (Employee e : employeeList) {
            departmentList.add(departmentService.getDepartment(e.getDept_id()));
        }
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("employee", employeeList);
        model.put("department", departmentList);

        return new ModelAndView("viewEmployee", "model", model);
    } 

viewEmployee.jsp

<table border="1px" bordercolor="black" width=80% align="center">
                <tr>
                    <td>Name</td>
                    <td>Gender</td>
                    <td>Salary</td>
                    <td>Department</td>
                    <td>Action</td>
                </tr>
                <c:forEach items="${model.employeeList}" var="element">

                    <tr>
                        <td><c:out value="${element.name}" /></td>
                        <td><c:out value="${element.gender}" /></td>

                        <td><c:out value="${element.salary}" /></td>

                        <td>display Department Name here </td>

                        <td><a
                            href="<c:url value="editEmployee.htm">  
                    <c:param name="emp_id" value="${element.id}"/>  
        </c:url>  
    ">Edit</a>
                            <a
                            href="<c:url value="deleteEmployee.htm">  
                    <c:param name="emp_id" value="${element.id}"/>  
        </c:url>  
    ">Delete</a>

                        </td>
                    </tr>

                </c:forEach>
            </table>

有任何帮助吗?我无法将地图内容显示到jsp页面。

1 个答案:

答案 0 :(得分:4)

应为<c:forEach items="${model.employee}" var="element">

此外,部门应该是员工的财产,以便您可以使用${employee.department.name}