簡體   English   中英

如何從字符串和列表的映射動態生成表 <String> 使用JSP和Servlet?

[英]How to generate table dynamically from the Map of String and List<String> using JSP and Servlet?

我有一個響應對象,其中包含Map的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>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
<%
// 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>
 <%
      }

 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM