【发布时间】:2014-04-13 00:42:02
【问题描述】:
我正在开发 Spring MVC 项目,其中我需要将一个对象从我的控制器传递到 JSP,然后我需要迭代该对象并将它们显示在 jsp 页面的表格中。
下面是我保存数据的类 -
public class DatacenterMachineMapping {
private Map<String, List<MachineMetrics>> datacenterMachines;
// getters and setters
}
public class MachineMetrics {
private String machineName;
private String t2_95;
private String t2_99;
private String syncs;
private String syncsBehind;
private String average;
// getters and setters
}
下面是我在控制器中的方法,我需要从中将对象传递给 JSP,然后在 JSP 中迭代该对象以在表中显示数据 -
@RequestMapping(value = "testOperation", method = RequestMethod.GET)
public Map<String, String> testData() {
final Map<String, String> model = new LinkedHashMap<String, String>();
// for datacenter 1
MachineMetrics metrics1 = new MachineMetrics();
metrics1.setAvg("10");
metrics1.setT2_95("100");
metrics1.setT2_99("200");
metrics1.setMachineName("machineA");
metrics1.setSyncs("100");
metrics1.setSyncsBehind("1000");
MachineMetrics metrics2 = new MachineMetrics();
metrics2.setAvg("20");
metrics2.setT2_95("200");
metrics2.setT2_99("300");
metrics2.setMachineName("machineB");
metrics2.setSyncs("200");
metrics2.setSyncsBehind("2000");
List<MachineMetrics> metrics = new LinkedList<MachineMetrics>();
metrics.add(metrics1);
metrics.add(metrics2);
DatacenterMachineMapping mappings = new DatacenterMachineMapping();
Map<String, List<MachineMetrics>> holder = new LinkedHashMap<String, List<MachineMetrics>>();
holder.put("dc1", metrics);
mappings.setDatacenterMachines(holder);
model.put("testing", mappings);
return model;
}
下面是我的 JSP 页面。我不确定如何在 JSP 页面中以这种方式使用上述 mappings 对象,以便我可以对其进行迭代并在表格中显示结果。
<body>
<table>
<thead>
<tr>
<th>Machine Name</th>
<th>T2_95</th>
<th>T2_99</th>
<th>Syncs</th>
<th>Syncs Behind</th>
<th>Average</th>
</tr>
</thead>
<tbody>
<!-- this gives me exception -->
<c:forEach items="${testing.value}" var="m">
<tr>
<td>${m.machineName}</td>
<td>${m.t2_95}</td>
<td>${m.t2_99}</td>
<td>${m.syncs}</td>
<td>${m.syncsBehind}</td>
<td>${m.average}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
我已经尝试使用如上所示的 JSTL,并且我试图在从 Controller 传递到 JSP 的映射中迭代 list,但不知何故它给了我一个异常 -
'${testing.value}' Property 'value' not found
在对 Map 中的列表进行迭代后,我的数据在 Datacenter 1 的表中应该如下所示-
Machine Name T2_95 T2_99 Syncs Syncs Behind Average
machineA 100 200 100 1000 10
machineB 200 300 200 2000 20
我在这里做错什么了吗?
我正在跟进我之前的问题here。在这个问题中,响应对象是不同的,因为我在这里有字符串和列表的映射。
【问题讨论】:
-
看这个问题几乎是类似的问题。 stackoverflow.com/questions/2848332/…
-
@RaviKumar:我已经看到并尝试过,但对我根本不起作用。我肯定做错了。如果可能的话,你能帮忙吗?
标签: java spring jsp spring-mvc jstl