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

岁酱吖の 提交于 2021-02-10 18:11:05

问题


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

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

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:

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

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


回答1:


Given below is a Minimal, Verifiable Example:

Dashboard.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);
    }
}

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:



来源:https://stackoverflow.com/questions/62118682/displaying-map-in-tabular-format-which-has-object-as-key-and-arraylist-as-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!