1

我已将 ArrayList 保存到会话对象中。我正在尝试使用

sriList = session.getAttribute("scannedMatches");

我收到编译时错误“无法从 Object 转换为 ArrayList”。如何从会话对象中取回我的 ArrayList。

4

3 回答 3

7

HttpSession#getAttribute()方法返回java.lang.Object

public java.lang.Object getAttribute(java.lang.String name)

您是否尝试投射返回的对象?

sriList = (ArrayList)session.getAttribute("scannedMatches");
于 2009-11-18T13:12:31.113 回答
3

你必须施放它。

sriList = (ArrayList)session.getAttribute("scannedMatches");
于 2009-11-18T13:12:42.080 回答
1

试试这个:

Object scannedMatchesObj = session.getAttribute("scannedMatches");
if ( scannedmatchesObj instanceOf List ){
    sriList = (ArrayList)scannedMatchesObj;
    //Do your stuff...
}
于 2009-11-18T13:16:37.073 回答