1

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 String datacenter;
    private List<MachineMetrics> metrics;

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

    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();
    mappings.setColo("dc1");
    mappings.setMetrics(metrics);

    return model;   
}

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>

            <!-- what to do here? -->

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

My data should look like this in the table for Datacenter 1-

Machine Name    T2_95   T2_99   Syncs   Syncs Behind    Average

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

Do I need to use JSTL for this? Or there are any better approach for this?

  • Yes you will need JSTL. That is the best approach. – Bart Mar 09 '14 at 20:24
  • @Bart ohh great.. Thanks.. Can you provide an example how would my above example will work if possible with JSTL? I have started working on Spring and JSP recently so still learning.. –  Mar 09 '14 at 20:26

1 Answers1

2

Import the JSTL library into your JSP page using

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

This will link the JSTL namespace to c. JSTL will make the forEach tag available which can be used to iterate over you collection.

make sure your objects follow the bean naming convention

<c:forEach items="${dc1.metrics}" 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>
Bart
  • 17,070
  • 5
  • 61
  • 80
  • Thanks Bart.. Then how my controller method will look like? I guess something has to be change there as well? Because I would like to pass `mappings` object to JSP page and then iterate that in JSP using JSTL. As I need to show which datacenter metrics is this.. As the above metrics is for `datacenter1`.. –  Mar 09 '14 at 20:36
  • Put `model.put("dc1", mappings)` before your `return` statement. I will adjust the example accordingly. – Bart Mar 09 '14 at 20:43
  • In the `mappings` object itself, I have a string `datacenter` which will define whether this object is for `datacenter1` or `datacenter2`. If I do it like this `model.put("dc1", mappings)` then how will it work for `dc2`? My ultimate goal is to have one table for one datacenter which is `dc1` and other table for second datacenter which is `dc2`.. And currently I have three datacenters.. so max it will be three tables.. –  Mar 09 '14 at 20:45
  • You can also create a collection of mappings. The concept must be clear now so you can apply it as you please. – Bart Mar 09 '14 at 20:47