如何使用JSP和Servlet?</string>从String of Map和List <string>动态生成表

时间:2014-01-17 03:01:45

标签: java jsp servlets html-table

我的响应对象包含地图的getter和setter -

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;
    }
}

在上面的对象中,我有一个Map of String and List<String>。在地图中,keys是我的表格标题,地图中的value是该标题的表格数据。

假设这是地图中的值 -

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

类似地,

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

然后我的表应该看起来像这样

FirstName   LastName
David       JOHN    
RON         PETER   
HELLO       TOM     

我需要动态生成上面的表,因为我将dataResponse对象传递给我的JSP页面,如下所述 -

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

以下是我在JSP中的表,我使用上面的对象以上述格式生成表

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

但不知何故,我的表格没有以我在上面的例子中展示的方式显示出来。所有键都在表头中正确显示,但所有值仅显示在第一列中。

所有键的列表大小都相同。

有没有想过如何做到这一点?

更新: -

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

这给了我 -

FirstName   LastName
David       RON        HELLO
JOHN        PETER      TOM

1 个答案:

答案 0 :(得分: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);

%GT;

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

这种方法更容易和建议。 如果您仍想坚持使用代码,可以使用jsp标签代替jstl标签来实现您的目标,如下所示

<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>
 <%
      }

 %>