我已将ArrayList保存到会话对象。我试图检索它使用当从HttpSession中检索ArrayList时,获取不能将对象转换为ArrayList错误
sriList = session.getAttribute("scannedMatches");
我得到编译时错误“无法从对象转换为ArrayList”。我如何从会话对象中取回我的ArrayList。
我已将ArrayList保存到会话对象。我试图检索它使用当从HttpSession中检索ArrayList时,获取不能将对象转换为ArrayList错误
sriList = session.getAttribute("scannedMatches");
我得到编译时错误“无法从对象转换为ArrayList”。我如何从会话对象中取回我的ArrayList。
的HttpSession#getAttribute()
方法返回java.lang.Object
:
public java.lang.Object getAttribute(java.lang.String name)
你尝试投返回的对象?
sriList = (ArrayList)session.getAttribute("scannedMatches");
你必须施放它。
sriList = (ArrayList)session.getAttribute("scannedMatches");
试试这个:
Object scannedMatchesObj = session.getAttribute("scannedMatches");
if (scannedmatchesObj instanceOf List){
sriList = (ArrayList)scannedMatchesObj;
//Do your stuff...
}
Just becuase(scannedmatchesObj instanceof List)为true,并不一定表示它是一个ArrayList。根据具体情况,在这里可能是一个更好的主意,将sriList声明为List,并相应地进行投票。 – joev 2009-11-18 15:35:01
+1'在HttpSession#的getAttribute()方法返回java.lang.Object继承:' – 2009-11-18 13:17:08
谢谢 - 通常我的IDE建议时,它是有道理的铸造,所以我认为这里一定有些不同。 – Ankur 2009-11-18 13:19:48
这是我们正在成为IDE依赖的标志吗?我记得用命令行javac编程我的第一个应用程序在记事本中...我的第一个BIG程序,我没有自动完成... – Zoidberg 2009-11-18 13:22:31