1

I have my response Object as which contains getter and setters for the Map -

public class DataResponse {

    private Map<String, List<String>> attributes = new LinkedHashMap<String, List<String>>();

    public Map<String, List<String>> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, List<String>> attributes) {
        this.attributes = attributes;
    }
}

In the above object, I have a Map of String and List<String>. In the map, keys are my table header and the value in the map is the table data for that header.

Suppose if this is the value in the map -

FirstName is the Key in the same Map
DAVID, RON, HELLO are the values in the map as the List for that key.

Similarly,

LastName is the Key in the same Map
JOHN, PETER, TOM are the values in the map as the List for the `LastName` key.

Then my Table should look like this

FirstName   LastName
David       JOHN    
RON         PETER   
HELLO       TOM     

I need to generate my above table dynamically as am passing my dataResponse object to my JSP page as mentioned below -

DataResponse dataResponse = some_code_here;
req.setAttribute("data", dataResponse);
WebUtil.forward(req, resp, this, "/admin/test.jsp");

And below is my table in JSP in which I am using my above object to generate the table in the above format

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

But somehow my tables are not getting shown in the way I am trying to show in my above example. All the keys are getting shown properly in the Table Headers but all the values are getting shown only in first column.

And size of the list will be same for all the keys.

Any thought how this can be done?

UPDATE:-

<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
    <TR>
        <c:forEach var="h" items="${data.attributes}">
            <TH>${h.key}</TH>
        </c:forEach>
    </TR>
    //iterate again
    <c:forEach var="h" items="${data.attributes}">
        <TR>
            <c:forEach var="headers" items="${h.value}">
                <TD>${headers}</TD>
            </c:forEach>

        </TR>
    </c:forEach>
</TABLE>

This gives me -

FirstName   LastName
David       RON        HELLO
JOHN        PETER      TOM
7
  • 2
    Move your 2nd loop inside <TR>...</TR> Commented Jan 17, 2014 at 3:07
  • Doesn't work that way as well. I have already tried those steps. See my updated question.. Commented Jan 17, 2014 at 3:17
  • I think u need to move td out -- <c:forEach var="h" items="${data.attributes}"> <TR><TD> <c:forEach var="headers" items="${h.value}"> ${headers} </c:forEach> </TD> </TR> Commented Jan 17, 2014 at 3:26
  • something like this <c:forEach var="h" items="${data.attributes}"> <TR> <TD> <c:forEach var="headers" items="${h.value}"> ${headers} </c:forEach> </TD> </TR> </c:forEach> Din't work.. :( Commented Jan 17, 2014 at 3:31
  • The way you organized your map - fields of the same object being stored under different keys and, as a result, in different lists - wouldn't allow for this form of iteration. The only thing that "links" FistName and LastName together is their position in their respective lists. Commented Jan 17, 2014 at 3:44

1 Answer 1

1
<%
// Create an ArrayList with test data
ArrayList list = new ArrayList();
Map person1 = new HashMap();
person1.put("name", "A");
person1.put("lastname", "A1";
list.add(person1);
Map person2 = new HashMap();
person2.put("name", "B");
person2.put("lastname", "B1");
list.add(person2);
Map person3 = new HashMap();
person3.put("name", "C");
person3.put("lastname", "");
list.add(person3);
pageContext.setAttribute("persons", list);

%>

<html>
  <head>
<title>Search result: persons</title>
  </head>
  <body bgcolor="white">
  Here are all persons matching your search critera:
<table>
  <TH>Name</th>
  <TH>Id</th>
  <c:forEach items="${persons}" var="current">
    <tr>
      <td><c:out value="${current.name}" /><td>
      <td><c:out value="${current.lastname}" /><td>
    </tr>
  </c:forEach>
</table>

This approach is easier and suggested. If u still want to stick with your code, you can use jsp tags instead of jstl tags to achieve your goal as follow

<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
<TR>
    <c:forEach var="h" items="${data.attributes}">
        <TH>${h.key}</TH>
    </c:forEach>
</TR>
//use jsp scriplets to acces both list simultaneoulsy
<% List data= request.getAttribute("data")==null?null:(List) request.getAttribute("data");
 List Names=data.get(0);
 List LastNames=data.get(1);
 for(int i=0;i<Names.length();i++){ %>
      <td><%=Names.get(i)%></td><td><%=LastNames.get(i)%></td>
 <%
      }

 %>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.