简体   繁体   English

以表格格式显示 map,其中 object 作为键,Arraylist 作为 ZEC407CCE6B649DAA8E32053 中的值

[英]Displaying map in tabular format which has object as key and Arraylist as value in jsp

MapKey mapKey = new MapKey(reqId, name, status);     
LinkedHashMap<Object, List<Dashboard>> map = new LinkedHashMap<>();

Mapkey Class:映射键 Class:

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 CLASS: POJO CLASS:

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

Input:输入:

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],...,..]

Expected Output:预期 Output:

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

I have a Map where key is an Object and value is List.Arraylist comprises of rows of each unique reqid as objects.我有一个 Map ,其中键是 Object ,值是 List.Arraylist 包含作为对象的每个唯一 reqid 的行。 In the map, keys are my table header and the value in the map is the table data for that header.Keys depends on the data from sql.It can be vary as per data.I want to display each key as table header and all its related data as rows of the table.I have written code in jsp to create table for each key.I am newbie in java development,so that is is what I could write.I need help to achieve the expected output. In the map, keys are my table header and the value in the map is the table data for that header.Keys depends on the data from sql.It can be vary as per data.I want to display each key as table header and all其相关数据作为表的行。我已经在 jsp 中编写了代码来为每个键创建表。我是 java 开发的新手,所以这就是我可以写的。我需要帮助来实现预期的 Z78E6221F6393D14CEDZ88666。

Given below is a Minimal, Verifiable Example:下面给出了一个最小的、可验证的示例:

Dashboard.java:仪表板.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: 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: 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);
    }
}

my_page.jsp: my_page.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>

Output: Output: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM