java - How to show multiple tables depending on key in the Map using JSTL? -
i have map of string , list of object contains each datacenter , machine. , passing object jsp controller , iterating in jsp page show data.
if map size one, able show data in jsp page , works fine.
now suppose if map size two, show 2 tables 1 each key in map. not able make work. maximum size of map can 3 use case.
below class holds data -
public class datacentermachinemapping { private map<string, list<machinemetrics>> datacentermachines; // getters , 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 , setters }
and below method in controller need pass object jsp , iterate object in jsp show data in table -
@requestmapping(value = "testoperation", method = requestmethod.get) public map<string, string> testdata() { final map<string, string> model = new linkedhashmap<string, string>(); // 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); // 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 object jsp return model; }
problem statement:-
as can see in above code, have 2 keys have 2 datacenters 1 dc1
, other dc2
. show 2 tables - 1 dc1
, machines , second table dc2
, machines shown below.
so data should after iterating 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 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 jsp page works fine map size equal one.. , not sure how iterate above mappings
object in such way in jsp page can show 2 tables above use case 1 each key shown above. possible 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 make jsp generic here? might possible can have 3 datacenters have two.
i following on previous question here helps me iterate map of size 1 not sure how have 2 separate tables 1 each key
update:-
this have tried , doesn't work 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 getting below exception , not sure wrong doing here? -
don't know how iterate on supplied "items" in <foreach>
try -
<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>
Comments
Post a Comment