迭代地图并在jsp中显示

我必须在页面中显示对象的地图.

Struts动作类如下:

public class DonorListAction extends Action{
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, 
                                 HttpServletResponse response) throws Exception
    {
        DonorDAO donorDAO = new DonorDAO(); 
        HashMap<Integer, DonorDetailsForm> donorMap =donorDAO.getDonorList();
        request.setAttribute("donorMap",donorMap);
        return mapping.findForward("success"); 
    }
}

jsp页面如下所示:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>

<html>
<HEAD><TITLE>List of Donors</TITLE></HEAD>
<body BGCOLOR="#FDF5E6">
<table align="center" border="1">
<tr>
<th align="center">Donor ID</th>
<th align="center">Name</th> 
<th align="center">Email Address</th>
<th align="center">Phone Number</th> 
</tr> 
<c:forEach var="donor" items="${donorMap}"> 
<tr>
<td align="right">${donor.key}</td>
<td align="right">${donor.value.firstName} ${donor.value.lastName}</td> 
<td align="right">${donor.value.emailAddress}</td>
<td align="right">${donor.value.phoneNumber}</td> 
</tr> 
</c:forEach> 
</table> 
</body>
</html> 

现在,当我执行此代码时,我得到:

javax.el.PropertyNotFoundException: Property 'key' not found on type java.lang.String
javax.el.BeanELResolver$BeanProperties.get(BeanELR esolver.java:193)
javax.el.BeanELResolver$BeanProperties.access$400( BeanELResolver.java:170)
javax.el.BeanELResolver.property(BeanELResolver.ja va:279)
javax.el.BeanELResolver.getValue(BeanELResolver.ja va:60)
javax.el.CompositeELResolver.getValue(CompositeELR esolver.java:54)
org.apache.el.parser.AstValue.getValue(AstValue.ja va:118)
org.apache.el.ValueExpressionImpl.getValue(ValueEx pressionImpl.java:186)
org.apache.jasper.runtime.PageContextImpl.propriet aryEvaluate(PageContextImpl.java:925)
org.apache.jsp.jsp.DisplayDonorList_jsp._jspx_meth _c_005fforEach_005f0(DisplayDonorList_jsp.java:121 )
org.apache.jsp.jsp.DisplayDonorList_jsp._jspServic e(DisplayDonorList_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:322)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:249)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
org.apache.struts.action.RequestProcessor.doForwar d(RequestProcessor.java:1063)
org.apache.struts.action.RequestProcessor.processF orwardConfig(RequestProcessor.java:386)
org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:229)
org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1194)
org.apache.struts.action.ActionServlet.doGet(Actio nServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doF ilter(ReplyHeaderFilter.java:96)

我做错了什么?

最佳答案 异常基本上是告诉var属性中保存的对象等于java.lang.String.所以看起来${donorMap}实际上是一个List< String>或者是String [].另外,方法getDonorList();表明它实际上返回一个List,而不是一个HashMap.

我不确定问题的根本原因,也许你没有运行你认为正在运行的代码,但以下

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>

很可怕这意味着您已经提取了taglib的原始JAR文件,并使用其松散内容和TLD文件混乱了webapp的类路径.它可能与其中一个相冲突.

我强烈建议清理webcontent和/ WEB-INF中的所有内容,这些内容与那些taglib有关,然后通过将未触及的JAR文件直接放在/ WEB-INF / lib文件夹中重做,然后在JSP中重新声明它们,如下所示:

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

也可以看看:

> Struts User Guide – configuring tag libraries

也就是说,提取的taglib文件的这种不良做法通常在roseindia.com上的Struts / JSTL教程中公开.确保您没有关注它们.即使没有,那么只需记住这个网站,并在您意外登陆该网站时立即按Ctrl W.

也可以看看:

> What is it with roseindia?

点赞