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

Beginner
  • 51
  • 6
  • **A side note:** follow [Java naming conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html) e.g. `PRICE` should be `price` and `STATUS` should be `status`. – Arvind Kumar Avinash May 31 '20 at 15:50
  • I suggest you read the _javadoc_ for interface [java.util.Map](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html) – Abra May 31 '20 at 15:57
  • @Abra I have read few docs and tutorials but those are very basic.It doesn't get near to what I want to do. – Beginner May 31 '20 at 16:02
  • @ArvindKumarAvinash Updated.Kindly look into it. – Beginner May 31 '20 at 16:03
  • Looks better but you still need to change `request_id` to `requestId` and `login_user` to `loginUser`. Coming to your problem, you need to create the `` [inside the JSTL block for iterating the map](https://stackoverflow.com/questions/9257240/how-to-iterate-hashmap-using-jstl-foreach-loop).
    – Arvind Kumar Avinash May 31 '20 at 16:08
  • @ArvindKumarAvinash Can you give me rough idea with the code if possible? – Beginner May 31 '20 at 16:15
  • @Beginner - I've posted a Minimal, Verifiable Example. I hope, it helps. – Arvind Kumar Avinash May 31 '20 at 17:01

1 Answers1

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: enter image description here

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thanks a lot to give me idea.Just an update the map is defined in jdbc class where I fetch data from sql db. – Beginner May 31 '20 at 17:03
  • You are most welcome. You need to call the DAO (the class where you have put JDBC code) in the servlet. For the purpose of demo, I've populated a `Map` object with example data, in the servlet. – Arvind Kumar Avinash May 31 '20 at 17:03
  • Ya I will go through it once..Any update or change I will msg here.Thanks a lot for the help. – Beginner May 31 '20 at 17:05
  • I was just running the code once and I got this error.HTTP Status 500 – Internal Server Error.Type Exception Report.Message An exception occurred processing [/Dashboard.jsp] at line [58].Description:The server encountered an unexpected condition that prevented it from fulfilling the request.The line is 58 is ${item.requestId}${item.loginUser}${item.price}${item.status} – Beginner May 31 '20 at 17:18
  • @Beginner - I suggest you copy my code as it is and run it. Then, understand it and make changes to learn faster. – Arvind Kumar Avinash May 31 '20 at 17:29
  • I just did. The error is something like this: https://stackoverflow.com/questions/43077139/the-server-encountered-an-unexpected-condition-that-prevented-it-from-fulfilling – Beginner May 31 '20 at 17:32
  • I posted the tested code and moreover, it is the bare minimum code to demonstrate how you can deal with your required data structure. You can create a new **Dynamic Web Project** in eclipse and put into it the code from my answer. – Arvind Kumar Avinash May 31 '20 at 17:46
  • Getters and setters are defined in POJO class for all attributes. These values are set in jdbc class when retrieved from sql db.These data are then stored in the map.That map is need to be retrieved and data need to be pulled from map and shown in tabular format. – Beginner May 31 '20 at 17:49
  • I understand your requirement clearly. The only place where you need to change in my code is the method, `populateData` where I've put the hardcoded values into `map` whereas you will call your DAO class function to assign data to it i.e. it will be something like `Map> map = yourDaoObj.getData()` where `yourDaoObj` is the object of your DAO class where you have JDBC code in the function, `public Map> getData(...) {...}`. – Arvind Kumar Avinash May 31 '20 at 17:56
  • Small change I need as per my requirement.I want to make collapsible table for each key.So like for 1st key the table must be collapsible on clicking on the key table.Whenever user clicks on key table the data/value present in the table should be collapsed on clicking on it. – Beginner Jun 01 '20 at 04:44
  • For the collapsible table, you need to learn JavaScript. It is not difficult but unfortunately, I do not have expertise in JavaScript at the moment. I suggest you post a new question regarding this requirement so that you can get some good answers from JavaScript experts. – Arvind Kumar Avinash Jun 01 '20 at 06:29