如何使用JSTL迭代字符串映射和对象列表中的列表? [英] How to iterate a List in a Map of String and List of object using JSTL?

查看:79
本文介绍了如何使用JSTL迭代字符串映射和对象列表中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Spring MVC项目,在该项目中,我需要将一个对象从Controller传递到JSP,然后需要迭代该对象并将其显示在jsp页面的表中.

I am working on Spring MVC project in which I need to pass an object from my Controller to JSP and then I need to iterate that object and show them in a table in jsp page.

下面是我的类,其中包含数据-

Below is my class which holds the data -

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
}

下面是我在Controller中的方法,我需要从中将一个对象传递给JSP,然后在JSP中迭代该对象以在表中显示数据-

And below is my method in my Controller from which I need to pass an object to JSP and then iterate that object in JSP to show the data in a table -

@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对象,以便我可以对其进行迭代并在表中显示结果.

And below is my JSP page.. And I am not sure how to use the above mappings object in such a way in the JSP page so that I can iterate it and show the result in a table.

<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,但是不知何故,它给了我一个例外--

I have tried using JSTL as shown above and I was trying to iterate the list in a map which I have passed from Controller to JSP but somehow it is giving me an exception as -

'${testing.value}' Property 'value' not found

在迭代Map-中的列表后,我的数据在Datacenter 1的表中应如下所示

My data should look like this in the table for Datacenter 1 after iterating the list in the Map-

Machine Name    T2_95   T2_99   Syncs   Syncs Behind    Average

machineA        100     200     100     1000            10
machineB        200     300     200     2000            20

我在这里做错什么了吗?

Anything wrong I am doing here?

我正在跟踪我之前的问题

I am following up on my previous question here. In this question response object is different as I have Map of string and list here.

推荐答案

我认为这应该可行.

<c:set var="entry" value="${testing.datacenterMachines}"></c:set>
<c:forEachn var="m" items="${entry.value}">
   <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>

这篇关于如何使用JSTL迭代字符串映射和对象列表中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆