如何使用jstl迭代地图?

时间:2014-03-28 05:45:14

标签: java spring jsp map jstl

有没有人可以解释一下如何使用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);

4 个答案:

答案 0 :(得分:4)

如下图所示:

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

使用以下键从地图获取列表值:

${map.department}

您可以遍历列表,如下所示:

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

答案 1 :(得分:0)

试试这个,

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>

答案 2 :(得分:0)

你正在写

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);

键作为字符串映射,值作为对象列表但是作为值存储在映射列表中的EmmployeeDeparment

所以为了开始工作,你需要改变:

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...

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

答案 3 :(得分:0)

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

取代 deptName 取代您的模型属性。