我有一个映射,其中存储了一个 Id 作为键,在该 Id 下我有一个 bean 类列表,其中设置并存储了所有 bean 属性。如何在我的java类中显示 map 的内容?我尝试了类似的方法,但没有获取列表值。
for (Entry<String, List<MyBean>> me : MyForm.getClientId().entrySet())
{
String key = me.getKey();
List<MyBean> valueList = me.getValue();
System.out.println(" Key: " + key);
System.out.print("Values: ");
for (MyBean s : valueList) {
System.out.println(" " + s.getFundId());//This comes as null even though there are values in the list
}
}
同样我想打印jsp页面上的内容。我该怎么做?
最佳答案
试试这个代码:
将结果映射设置为 Servlet 中的请求属性,如下所示
request.setAttribute("myMap", map);
RequestDispatcher des = request.getRequestDispatcher("test.jsp");
des.forward(request, response);
您可以使用不同的方法将值传递到 JSP 页面。
之后就可以使用 JSTL Tag Library在 JSP 页面中读取该值。
您需要包含 jstl jar file到您的类路径中并将以下行添加到 jsp 以在页面中添加标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
当您需要读取值时,在 JSP 中使用此代码
<c:forEach var="beanList" items="${myMap}">
<c:forEach var="bean" items="${beanList['value']}">
<c:out value="${bean.fundId}"></c:out>
</c:forEach>
</c:forEach>
其中 fundId
是您的 bean 属性名称。
希望这会对您有所帮助。
关于java - 在 Java 中打印 Map<String,List<Bean>> 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32477071/