关于servlet、jsp相关的问题

来源:百度知道 编辑:UC知道 时间:2024/06/28 15:41:36
DAO中的O所对应的类Person有id和name等属性,并有setter和getter方法
在DAO中有对数据库更新的方法update(),该方法返回的ArrayList类型集合
而在servlet中我将该集合放入request中, 即
DAO sss = new DAO()
request.setAttribute("haha",sss.update())
现在在显示结果的jsp页面中我要想将request中的ArrayList集合取出来,并取出集合中的每个元素(也就是Person的引用对象)所对应的相关属性
但问题是我这样写:
((ArrayList)request.getAttribute("haha")).get(0).getName();
此时在get(0)那里报错 请各位高手帮帮忙吧 哭死我了 郁闷死了
拜托写的详细点 我不懂啊 具体是哪里

你都写到一条语句上了,这样写很容易出错,分开一步一步写试试:

ArrayList list=(ArrayList)request.getAttribute("haha");//取出集合
Person p=(Person)list.get(o); //取出集合中的一个Bean
String name=p.getName(); //取出Bean中的属性name
(上面的不一定对,因为不知道具体的程序)
如果不对,你按照你的程序,分开写再试试。

((Person)((ArrayList)request.getAttribute("haha")).get(0)).getName();
看行否。

get(0)之后还要再次强制转型

我好久没弄java了……如果你前边的setAttribute和getAttribute没错,的确得到了ArrayList的话,你没有对ArrayList进行类型转换,怎么可能有getName的方法?你要取数据库的字段没转成相应的javabean怎么能getName?

update方法返回List吗?