Struts访问ejb返回collection结果后,如何在jsp显示的问题

05-03-10 ctrlaomao0382
我最近在学习struts和ejb,建立了一个小的程序,目前遇到显示记录时遇到一点问题,请各位帮忙指点:

想实现的功能很简单,调用ejb中的findAll()方法返回数据库中的所有记录,返回到struts的action类里,返回的数据类型collection,然后在jsp中通过<logic:iterate>标签将记录循环显示出来,但运行后总是报错,错误如下:

javax.servlet.ServletException: Cannot create iterator for this

collection

org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageCon

textImpl.java:825)

org.apache.jasper.runtime.PageContextImpl.handlePageException(PageConte

xtImpl.java:758)

org.apache.jsp.default_jsp._jspService(default_jsp.java:160)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)

javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.j

ava:324)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292

)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)

javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.ja

va:1058)

org.apache.struts.action.RequestProcessor.processForwardConfig(RequestP

rocessor.java:451)

org.apache.struts.action.RequestProcessor.processActionForward(RequestP

rocessor.java:401)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java

:279)

org.apache.struts.action.ActionServlet.process(ActionServlet.java:1420)

org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:520)

javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

root cause

javax.servlet.jsp.JspException: Cannot create iterator for this

collection

org.apache.struts.taglib.logic.IterateTag.doStartTag(IterateTag.java:31

0)

org.apache.jsp.default_jsp._jspService(default_jsp.java:99)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)

javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.j

ava:324)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292

)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)

javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.ja

va:1058)

org.apache.struts.action.RequestProcessor.processForwardConfig(RequestP

rocessor.java:451)

org.apache.struts.action.RequestProcessor.processActionForward(RequestP

rocessor.java:401)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java

:279)

org.apache.struts.action.ActionServlet.process(ActionServlet.java:1420)

org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:520)

javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

我换了各种方法来调试,但归根到底还是报这个错。

下面是我的action类和jsp显示的代码,各位帮忙看看,问题出在哪?

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception {

System.out.println("entering CtrAction.execute()");

CtrForm ctrForm = (CtrForm) form;

CtrEJBFacade cf = new CtrEJBFacade();

Collection c = cf.findAll();

ArrayList array = new ArrayList(c);

System.out.println(array.size());

request.setAttribute("ctrFrom",array);

return (mapping.findForward("default"));

}

下面是jsp的:

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

<html>

<head>

<meta name = "Generator" content = "Easy Struts Xslt

generator for Eclipse (http://easystruts.sf.net).">

<title>记录显示页面</title>

</head>

<body bgcolor="black">

<table border="0" width="100%">

<tr>

<td algin="center">编号</td>

<td algin="center">姓名</td>

<td algin="center">新建</td>

<td algin="center">编辑</td>

<td algin="center">删除</td>

</tr>

<logic:present name="ctrForm"

scope="request"></logic:present>

<logic:iterate name="ctrForm" id="ctr"

type="com.ctr.struts.form.CtrForm">

<tr>

<td algin="center"><bean:write name="ctr" property="id"

/></td>

<td algin="center"><bean:write name="ctr" property="name"

/></td>

<td algin="center">

<html:link page="/ctr.do?action=add">新建</html:link>

</td>

<td algin="center">

<html:link

page="/ctr.do?action=update">编辑</html:link>

</td>

<td algin="center">

<html:link

page="/ctr.do?action=delete">删除</html:link>

</td>

</tr>

</logic:iterate>

</table>

</body>

</html>

我浪费了很多时间,希望有人能指点一下,谢谢了!

1
ctrlaomao0382
2005-03-10 16:45
刚才的jsp的代码没有显示出来。如下:

<logic:present name="ctrForm" scope="request"></logic:present>

<logic:iterate name="ctrForm" id="ctr" type="com.ctr.struts.form.CtrForm">

<tr>

<td algin="center"><bean:write name="ctr" property="id" /></td>

<td algin="center"><bean:write name="ctr" property="name" /></td>

<td algin="center">

<html:link page="/ctr.do?action=add">新建</html:link>

</td>

<td algin="center">

<html:link page="/ctr.do?action=update">编辑</html:link>

</td>

<td algin="center">

<html:link page="/ctr.do?action=delete">删除</html:link>

</td>

</tr>

</logic:iterate>

我浪费了很多时间了,请高人帮忙指点一下,谢谢!

SportsBaby1980
2005-03-11 10:05

这个应该放在SessionBean里做,

让SB返回ArrayList

dog5214
2005-03-11 18:16
我也是菜鸟,不过我也说说我的意见吧。不知道你为什么要把查询的结果放在Collection中,然后有放在ArrayList中。我对Collection不是很了解,很少用。一般情况下使用ArrayList来存放object。然后在jsp中从

session中读出,使用iterate显示。 你可以试试把结果放在ArrayList中吧,然后在jsp中读出。

shuangang
2006-02-11 11:06
Collection c=null;

ArrayList array=new ArrayList();

try {

c = home.findAll();

array.addAll(c);

} catch (FinderException ex) {

System.out.println(ex);

}

request.setAttribute("array",array);

猜你喜欢
2Go 1 2 下一页