tags:

views:

69

answers:

1

I have to display a map of objects in a page.

The Struts action class is as follows:

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"); 
    }
}

The jsp page looks like this:

<!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> 

Now when I execute this code I get:

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)

What mistake am I doing?

+2  A: 

The exception is basically telling that the object which is hold in var attribute equals to java.lang.String. So it look like that ${donorMap} is actually a List<String> or maybe a String[]. Also, the method getDonorList(); suggests that it actually returns a List, not a HashMap.

I'm not sure about the root cause of the problem, maybe you're not running the code you think you're running, but the following

<%@ 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"%>

is scary. This would mean that you've extracted the taglib's original JAR file and cluttered the webapp's classpath with its loose contents and TLD files. It might have conflicted with one and other.

I strongly recommend to cleanup everything in webcontent and /WEB-INF which is somehow related to those taglibs and then redo by putting the untouched JAR files straight in the /WEB-INF/lib folder and redeclare them in JSP's as follows:

<%@ 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"%>

See also:


That said, this bad practice of extracted taglib files is typically exposed on Struts/JSTL tutorials at roseindia.com. Ensure that you are not following them. Even if not, then just keep this site in mind and immediately hit Ctrl+W whenever you accidently land at that site.

See also:

BalusC
Thank you so much. After I redeclare the taglibs it works fine.
javaaz
You're welcome :) Don't forget to mark the answer accepted. Also see http://stackoverflow.com/faq
BalusC