java—在jsp中以对象为键、arraylist为值的表格格式显示Map

44u64gxh  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(417)
MapKey mapKey = new MapKey(reqId, name, status);     
LinkedHashMap<Object, List<Dashboard>> map = new LinkedHashMap<>();

mapkey类:

public class MapKey {
      private Integer id;
      private String name;
      private Integer status;

       public MapKey(Integer id, String name,Integer status) {
        this.id = id;
        this.name = name;
        this.status=status;
      }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
 //All getters and setters

pojo类:

public class Dashboard {
    int requestId;
    String loginUser;                  
    int price;                                     
    int status;      
    public int getrequestId() {
        return requestId;
    }
    public void setrequestId(int requestId) {
        requestId= requestId;
    }
     //All getters and setters

jsp代码:

<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;"> 
 <TR>     
  <c:forEach var="entry" items="${map}">     
     <TH>${entry.key}</TH>      
  </c:forEach>
 </TR>
 //iterate again
 <c:forEach var="entry" items="${map}">
   //entry.value is ArrayList so we can iterate with c:forEach
  <c:forEach var="headers" items="${entry.value}">
   <TR>         
      <TD>${headers}</TD>         
   </TR>
  </c:forEach>
</c:forEach>
</TABLE>

输入:

MapKey [reqid=123, name=A,status=1]:[Dashboard [reqid=123, NAME=A, PRICE=5,STATUS=2],Dashboard [reqid=123, NAME=A, PRICE=10,STATUS=3],...,..]
MapKey [reqid=456, name=B,status=2]:[Dashboard [reqid=456, NAME=B, PRICE=20,STATUS=3],Dashboard [reqid=456, NAME=B, PRICE=25,STATUS=2],...,..]

预期产量:

123  A   1   ///Table header 
123 A   5  2
123 A  10  3
//N no of rows 

456 B 2    ///Table header 
456 B  20  3
456 B  25  2
//N no of rows

我有一个Map,其中key是一个对象,value是list.arraylist由每个惟一的reqid作为对象的行组成。在Map中,键是我的表头,Map中的值是该表头的表数据。键取决于sql中的数据。它可以根据数据而变化。我想将每个键显示为表头,将其所有相关数据显示为表的行。我在jsp中编写了为每个键创建表的代码。我是java开发的新手,所以这就是我能写的。我需要帮助来实现预期的输出。

oknwwptz

oknwwptz1#

下面给出了一个最小的、可验证的示例:
Jmeter 板.java:

package beans;

public class Dashboard {
    int requestId;
    String loginUser;                  
    int price;                                     
    int status;
    public Dashboard(int requestId, String loginUser, int price, int status) {
        this.requestId = requestId;
        this.loginUser = loginUser;
        this.price = price;
        this.status = status;
    }
    public int getRequestId() {
        return requestId;
    }
    public String getLoginUser() {
        return loginUser;
    }
    public int getPrice() {
        return price;
    }
    public int getStatus() {
        return status;
    } 
}

mapkey.java文件:

package beans;

public class MapKey {
    private Integer id;
    private String name;
    private Integer status;

    public MapKey(Integer id, String name, Integer status) {
        this.id = id;
        this.name = name;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getStatus() {
        return status;
    }
}

appcontroller.java:

package servlets;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Dashboard;
import beans.MapKey;

@WebServlet("/Report")
public class AppController extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        populateData(request, response);
        RequestDispatcher view = request.getRequestDispatcher("my_page.jsp");
        view.forward(request, response);
    }

    public void populateData(HttpServletRequest request, HttpServletResponse response) {
        Map<MapKey, List<Dashboard>> map = new LinkedHashMap<>();
        map.put(new MapKey(123, "A", 1), List.of(new Dashboard(123, "A", 5, 2), new Dashboard(123, "A", 10, 3)));
        map.put(new MapKey(456, "B", 2), List.of(new Dashboard(456, "B", 20, 3), new Dashboard(456, "B", 25, 2)));
        request.setAttribute("reportMap", map);
    }
}

我的页面.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
    <head>
        <title>Applicant Information</title>
    </head>
    <body>
        <table border="1">
            <c:forEach var="map" items="${reportMap}">
                <tr>
                    <td>
                        <table border="1">
                            <tr>
                                <th>${map.key.id}</th><th>${map.key.name}</th><th>${map.key.status}</th>
                            </tr>
                        </table>
                    </td>
                    <td>
                        <table border="1">
                            <c:forEach var="item" items="${map.value}">
                                <tr>
                                    <td>${item.requestId}</td><td>${item.loginUser}</td><td>${item.price}</td><td>${item.status}</td>
                                </tr>
                            </c:forEach>
                        </table>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

输出:

相关问题