Katana24
Katana24

Reputation: 8959

Adding an array list to a session

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.

Cheers

Upvotes: 1

Views: 12575

Answers (1)

Giordano Maestro
Giordano Maestro

Reputation: 341

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
 }

Upvotes: 2

Related Questions