Question

Could anyone please explain me about How to iterate this map separately department and reporting to in the below example using jstl.

Map<String, List<Object>> map = new HashMap<String, List<Object>>();
    List<EmployeeDeparment> department = Emp.getEmployeeDepartment(EmployeeId);
    map.put("department", department);
    map.put("allreportingTo", allreportingTo);
    map.put("alldepartments", alldepartments);
request.setAttribute("map", map);
Was it helpful?

Solution

As shown below:

<c:forEach var="entry" items="${map}">
  Key: <c:out value="${entry.key}"/>
  Value: <c:out value="${entry.value}"/>
</c:forEach>

To get list value from the map using the key as below:

${map.department}

And you can iterate over the list, as shown below:

<c:forEach items="${map.department}" var="element"> 
  <tr>
    <td>${element.deptName}</td>
  </tr>
</c:forEach>

OTHER TIPS

try this,

List<String> list = new ArrayList<String>(map.keySet());
request.setAttribute("list", list);

<c:forEach< items="${list}" var="entry">
    <c:forEach items=${map[entry]} var="employeeDept">
        <c:out value ="${employeeDept.name}"/> 
// get some more values like this
    </c:forEach>
</c:forEach>

Your are writing

Map<String, List<Object>> map = new HashMap<String, List<Object>>();
List<EmployeeDeparment> department = Emp.getEmployeeDepartment(EmployeeId);
List<EmployeeDeparment> department = Emp.getEmployeeDepartment(EmployeeId);
map.put("department", department);

Map of key as String and value as List of Object but you are storing as value in map List of EmmployeeDeparment

So in order to get into work you nedd to change like:

Map<String,  List<EmployeeDeparment>> map = new HashMap<String,  List<EmployeeDeparment>>();
List<EmployeeDeparment> department = Emp.getEmployeeDepartment(EmployeeId);
List<EmployeeDeparment> department = Emp.getEmployeeDepartment(EmployeeId);
map.put("department", department);
and  your remain code...

and

<c:forEach items="${map}" var="m">
    <c:forEach items="${map.value}" var="list">
        ${list.id }
        ${list.name }
    </c:forEach>
</c:forEach>
<c:forEach items="${map.department}" var="dept">
     ${dept.deptName}                   
</c:forEach>

Insted of deptName take your model attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top