迭代Hashmap的列表,它也是使用jstl的hashmap列表

时间:2014-05-17 19:26:49

标签: java jsp foreach

考虑下面的迭代:requestScope.scriptdataset的类型为

List<HashMap<String, List<HashMap<String, Object>>>>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<c:forEach var="objdatatable" items="${requestScope.scriptdataset}">

<c:if test="${not empty objdatatable['vwcmstreevwdata']}">
<c:forEach var="objrecord" items="${objdatatable['vwcmstreevwdata']}">

<c:forEach var="record" items="${objrecord}">

<c:out value="${record['ChildName']}"/>

</c:forEach>

</c:forEach>

</c:if>

</c:forEach>

我想通过键获取最里面的hashmap的值,但它失败了,如下所示。

  

2014年5月17日下午8:51:16 org.apache.catalina.core.ApplicationDispatcher   调用SEVERE:servlet jsp的Servlet.service()抛出异常   javax.el.PropertyNotFoundException:找不到属性'ChildName'   输入java.util.HashMap $ Entry at   javax.el.BeanELResolver $ BeanProperties.get(BeanELResolver.java:266)     在   javax.el.BeanELResolver $ BeanProperties.access 300 $(BeanELResolver.java:243)     在javax.el.BeanELResolver.property(BeanELResolver.java:353)at   javax.el.BeanELResolver.getValue(BeanELResolver.java:97)at at   org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)     在org.apache.el.parser.AstValue.getValue(AstValue.java:183)at   org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)     在   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)     在   org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fout_005f0(menudetails_jsp.java:221)     在   org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fforEach_005f2(menudetails_jsp.java:190)     在   org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fforEach_005f1(menudetails_jsp.java:150)     在   org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fforEach_005f0(menudetails_jsp.java:110)     在   org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspService(menudetails_jsp.java:72)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)at   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)     在   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)at   org.apache.catalina.core.ApplicationFilterChain.internalDo

在上面显示的代码中,如果将<c:out value="${record['ChildName']}"/>更改为<c:out value="${record}"/>
它显示内部列表的所有数据,以确保迭代正常,但我应该如何通过键获取内部hashmap的值。任何帮助将不胜感激。

修改

在上面的代码中

如果我将<c:out value="${record['ChildName']}"/>更改为<c:out value="${record.key}"/>

显示所有键

如果我将<c:out value="${record['ChildName']}"/>更改为<c:out value="${record.value}"/>

显示所有值

如果我将<c:out value="${record['ChildName']}"/>更改为

<c:forEach var="entry" items="${record}" >

<c:out value="${entry.key}"/>

</c:forEach>

抱怨道:

May 18, 2014 10:02:16 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;
    at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:312)
    at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:276)
    at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:193)
    at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:291)
    at org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fforEach_005f3(menudetails_jsp.java:226)
    at org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fforEach_005f2(menudetails_jsp.java:190)
    at org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fforEach_005f1(menudetails_jsp.java:150)
    at org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspx_meth_c_005fforEach_005f0(menudetails_jsp.java:110)
    at org.apache.jsp.WEB_002dINF.views.menudetails_jsp._jspService(menudetails_jsp.java:72)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

1 个答案:

答案 0 :(得分:0)

我更改了代码FROM

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<c:forEach var="objdatatable" items="${requestScope.scriptdataset}">

<c:if test="${not empty objdatatable['vwcmstreevwdata']}">
<c:forEach var="objrecord" items="${objdatatable['vwcmstreevwdata']}">

<c:forEach var="record" items="${objrecord}">

<c:out value="${record['ChildName']}"/>

</c:forEach>

</c:forEach>

</c:if>

</c:forEach>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<c:forEach var="objdatatable" items="${requestScope.scriptdataset}">

<c:if test="${not empty objdatatable['vwcmstreevwdata']}">
<c:forEach var="objrecord" items="${objdatatable['vwcmstreevwdata']}">

<c:out value="${objrecord['ChildName']}"/>

</c:forEach>

 </c:if>

</c:forEach>

它正在发挥作用。 JSTL的“反JSP Scriptlet”阵营支持者的成员必须解释。 感谢。