【问题标题】:How to generate table dynamically from the Map of String and List<String> using JSP and Servlet?如何使用 JSP 和 Servlet 从 String 和 List<String> 的 Map 动态生成表?
【发布时间】:2014-01-17 03:01:44
【问题描述】:

我的响应对象包含地图的 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&lt;String&gt;。在地图中,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

【问题讨论】:

  • 将你的第二个循环移动到&lt;TR&gt;...&lt;/TR&gt;
  • 也不是这样工作的。我已经尝试过这些步骤。查看我更新的问题..
  • 我认为你需要将 td 移出 -- ${headers}
  • 类似这样的&lt;c:forEach var="h" items="${data.attributes}"&gt; &lt;TR&gt; &lt;TD&gt; &lt;c:forEach var="headers" items="${h.value}"&gt; ${headers} &lt;/c:forEach&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/c:forEach&gt; 不行.. :(
  • 您组织地图的方式——同一对象的字段存储在不同的键下,因此存储在不同的列表中——不允许这种形式的迭代。唯一将FistNameLastName“链接”在一起的是它们在各自列表中的位置。

标签: java jsp servlets html-table


【解决方案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>

这种方法更简单且建议使用。 如果你仍然想坚持你的代码,你可以使用 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>
 <%
      }

 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2016-01-12
    • 2023-03-23
    • 2018-08-02
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多