Spring MVC Pass multiple list from controller to jsp

19,265

it should be <c:forEach items="${model.employee}" var="element">

Also department should be a property of Employee , so that you can use ${employee.department.name}

Share:
19,265
nebula
Author by

nebula

Hmmm. Programming :)

Updated on June 17, 2022

Comments

  • nebula
    nebula almost 2 years

    I have two objects Employee and Department Employee contains the dept_id. What i want to do is display the content of Employee in a table in jsp page. But instead of displaying dept_id i want to display dept_name from Department table. So far i have my controller method as:

    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>
    

    Any help? I am not being able to display the map content to the jsp page.