Question

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.

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

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
}

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

Problem Statement:-

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

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>

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.

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

UPDATE:-

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;
Was it helpful?

Solution

Try this -

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top