복붙노트

[SPRING] JSTL을 사용하여지도의 키에 따라 여러 테이블을 표시하는 방법?

SPRING

JSTL을 사용하여지도의 키에 따라 여러 테이블을 표시하는 방법?

나는 각각의 데이터 센터와 그 기계를 포함하는 String Map과 Object of List를 가지고있다. 그리고 현재이 컨트롤러를 JSP에서이 개체를 전달하고 JSP 페이지에서 반복하여 데이터를 표시합니다.

맵 크기가 1이면 JSP 페이지에 데이터를 표시 할 수 있으며 제대로 작동합니다.

이제지도 크기가 2라면지도의 각 키에 대해 두 개의 표를 표시하고 싶습니다. 이것이 내가 일을 할 수없는 것이다. 나의 유스 케이스의 경우지도의 최대 크기는 3이 될 수 있습니다.

아래는 데이터를 저장하는 클래스입니다.

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
}

아래는 내 컨트롤러에서 JSP로 객체를 전달한 다음 JSP의 객체를 반복하여 테이블에 데이터를 표시하는 방법입니다.

@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에 대한 두 번째 테이블을 보여주고 싶습니다.

그래서 내 데이터는 테스팅 객체를 반복 한 후에 이렇게 보일 것입니다.

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

아래는 맵 크기가 1 인 경우에만 잘 작동하는 JSP 페이지입니다. 그리고 위의 매핑 객체를 JSP 페이지에서 반복적으로 반복하는 방법을 잘 모르겠습니다. 위의 사용 사례에 대해 두 개의 테이블을 표시 할 수 있습니다. 위에 표시된대로 각 키에 하나씩. 가능한가요?

<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 Generic을 어떻게 만들 수 있습니까? 가능하다면 현재 3 개의 데이터 센터를 가질 수 있습니다.

나는 나의 이전 질문에 대해 다음과 같이 설명한다. 하나는 크기 맵을 반복하는 데 도움이되지만 각 키에 대해 두 개의 별개의 테이블을 가질 수있는 방법은 확실하지 않다.

최신 정보:-

이것은 내가 시도한 것이며 그것이 나를 위해 작동하지 않습니다 -

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

그리고 나는 예외 아래에있게되고 있으며 나는 여기서 무엇을 잘못하고 있는지 확신하지 못한다. -

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

해결법

  1. ==============================

    1.이 시도 -

    이 시도 -

    <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>
    
  2. from https://stackoverflow.com/questions/22293665/how-to-show-multiple-tables-depending-on-key-in-the-map-using-jstl by cc-by-sa and MIT license