2012-12-12 31 views
1

我试着在会话存储一个数组列表,像这样:添加一个数组列表到会话

private Map session = ActionContext.getContext().getSession(); 

数组列表如下:

private ArrayList<Integer> numbersEntered = new ArrayList<Integer>(); 

如果数组列表不在会话中已经存在它,但是Im在将数据添加到数组列表中并使用该数据更新会话时遇到问题。所以 - 我的问题是如何获得已经在会话中的内容,暂时存储它,根据用户输入添加它并重新添加到会话中?

if (!session.containsKey(arrayListID)) 
{ 
// Place the number the user entered into the session 
session.put(arrayListID, numbersEntered); 
} else { 

// Retrieve session data 
} 

我检索最初存储的内容并将其放入一个字符串中,但因为它是anm数组列表,所以它被存储为:[12]。我不想转换它或分割字符串...让我知道如果你需要更多的信息在这里。

干杯

回答

2
if (!session.containsKey(arrayListID)) 
{ 
// Place the number the user entered into the session 
session.put(arrayListID, numbersEntered); 
} else { 
    ArrayList<Integer> list = (ArrayList<Integer>) session.get(arrayListID); 
    list.add(1 /* what you want */); 
// Retrieve session data 
} 
+0

.getAttribute()是未定义地图 – Katana24

+0

对不起,我以为你在HttpSession中使用ArrayList的 –

+0

名单 =(ArrayList的)session.get(arrayListID); –