5

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

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 页面。

4

1 回答 1

4

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

部门也应该是 Employee 的属性,以便您可以使用${employee.department.name}

于 2012-12-10T06:12:47.250 回答