如何使用JSTL根据Map中的键显示多个表? [英] How to show multiple tables depending on key in the Map using JSTL?

查看:169
本文介绍了如何使用JSTL根据Map中的键显示多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串映射和一个包含每个数据中心及其机器的对象列表。我目前正在从Controller中将此对象传递给JSP,然后我在JSP页面中迭代它以显示数据。

I have a Map of String and List of Object which contains each datacenter and its machine. And I am currently passing this object to JSP from my Controller and then I am iterating it in JSP page to show the data.

如果Map大小为1,那么我可以在JSP页面中显示数据并且工作正常。

If the Map size is one, then I am able to show the data in a JSP page and it works fine.

现在假设如果Map大小是2,那么我想为地图中的每个键显示两个表。这是我无法开展工作的原因。对于我的用例,地图的最大大小可以是3。

Now suppose if the Map size is two, then I would like to show two tables one for each key in the map. This is what I am not able to make work. Maximum size of map can be 3 for my use case.

以下是我保存数据的类 -

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> metricsA = new LinkedList<MachineMetrics>();
    metricsA.add(metrics1);
    metricsA.add(metrics2);

    // for datacenter 2
    MachineMetrics metrics3= new MachineMetrics();
    metrics3.setAvg("30");
    metrics3.setT2_95("300");
    metrics3.setT2_99("300");
    metrics3.setMachineName("machineC");
    metrics3.setSyncs("300");
    metrics3.setSyncsBehind("3000");

    MachineMetrics metrics4 = new MachineMetrics();
    metrics4.setAvg("40");
    metrics4.setT2_95("400");
    metrics4.setT2_99("400");
    metrics4.setMachineName("machineD");
    metrics4.setSyncs("400");
    metrics4.setSyncsBehind("4000");

    List<MachineMetrics> metricsB = new LinkedList<MachineMetrics>();
    metricsB.add(metrics3);
    metricsB.add(metrics4);     

    DatacenterMachineMapping mappings = new DatacenterMachineMapping();
    Map<String, List<MachineMetrics>> holder = new LinkedHashMap<String, List<MachineMetrics>>();
    holder.put("dc1", metricsA);
    holder.put("dc2", metricsB);

    mappings.setDatacenterMachines(holder);     

    model.put("testing", mappings); // passing this object to jsp

    return model;   
}

问题陈述: -

正如您在上面的代码中看到的,我有两个密钥,因为我有两个数据中心,一个是 dc1 ,其他是 DC2 。所以我想展示两个表 - 一个用于 dc1 及其机器和第二个表用于 dc2 及其机器为如下所示。

As you can see in the above code, I have two keys as I have two datacenters one is dc1 and other is dc2. So I would like to show two tables - one for dc1 and its machines and second table for dc2 and its machines as shown below.

因此,在迭代测试对象后,我的数据应该如下所示 -

So my data should look like this after iterating the testing object-

For DC1

Machine Name    T2_95   T2_99   Syncs   Syncs Behind    Average

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

For DC2

Machine Name    T2_95   T2_99   Syncs   Syncs Behind    Average

machineC        300     300     300     3000            30
machineD        400     400     400     4000            40

以下是我的JSP页面,仅适用于map size等于1.我不知道如何在JSP页面中以这种方式迭代上面的 mappings 对象,以便我可以为上面显示两个表每个用例一个用例如上所示。这可能吗?

And below is my JSP page which works fine only for map size equal to one.. And I am not sure how to iterate the above mappings object in such a way in the JSP page so that I can show two tables for my above use case one for each key as shown above. Is this possible to do?

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

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

        </tbody>
    </table>
</body>

任何想法如何在这里制作我的JSP通用?因为我可能有三个数据中心,因为目前我有两个。

Any idea how can I make my JSP generic here? As it might be possible I can have three datacenters as currently I have two.

我正在跟进我之前的问题此处这有助于我迭代一张大小为1的地图,但不确定如何为每个键分别设置两个单独的表

I am following up on my previous question here which helps me to iterate a map of size one but not sure how would I have two separate tables one for each key

更新: -

这是我试过的,它对我不起作用 -

This is what I have tried and it doesn't work for me -

<c:forEach var="e" items="${testing}">
  <h3>For <c:out value="${e.key}"/></h3>
    <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>
          <c:forEach var="m" items="${e.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>
        </tbody>
    </table>
</c:forEach>

而且我得到的是异常,我不确定我在这里做错了什么? -

And I am getting below exception and I am not sure what wrong I am doing here? -

Don't know how to iterate over supplied "items" in &lt;forEach&gt;


推荐答案

试试这个 -

<c:forEach var="e" items="${entry}">
  <h3>For <c:out value="${e.key}"/></h3>
    <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>
          <c:forEach var="m" items="${e.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>
        </tbody>
    </table>
</c:forEach>

这篇关于如何使用JSTL根据Map中的键显示多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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