迭代地图并在jsp中显示

时间:2010-07-09 05:29:55

标签: jsp struts

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

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)

我做错了什么?

1 个答案:

答案 0 :(得分:2)

该异常基本上是告诉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"%>

另见:


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

另见: