java 将数组列表添加到会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13843573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:09:43  来源:igfitidea点击:

Adding an array list to a session

javastruts2

提问by Katana24

Im trying to store an array list in session like so:

我试图在会话中存储一个数组列表,如下所示:

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

The array list looks like this:

数组列表如下所示:

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

If the array list doesn't already exist in session it is added but Im having problems adding new data to the array list and updating the session with that data. So - my problem is how do I get what is already in session, store it temporarily, add to it based on the user input and re-add to the session?

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

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

// Retrieve session data
 }

I retrieve what was stored initially and placed it in a string but because it was anm array list it was stored like: [12]. I don't want to have to convert it or split the string...Let me know if you need more info here.

我检索了最初存储的内容并将其放在一个字符串中,但因为它是一个数组列表,所以它的存储方式如下:[12]。我不想转换它或拆分字符串...如果您需要更多信息,请告诉我。

Cheers

干杯

回答by Giordano Maestro

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
 }