gpt4 book ai didi

java - 如何使用 JSP 和 Servlet 从 String 和 List 的映射动态生成表?

转载 作者:太空宇宙 更新时间:2023-11-04 07:07:15 25 4
gpt4 key购买 nike

我有我的响应对象,其中包含 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 上,keys是我的表头和 value map 中是该标题的表格数据。

假设这是否是 map 中的值 -

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

%>

关于java - 如何使用 JSP 和 Servlet 从 String 和 List<String> 的映射动态生成表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21177062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com