jpganz18
jpganz18

Reputation: 5858

how can I maintain a List of objects in memory between different methods calls?

I have this problem,using spring mvc, there is a List (or an object) that I generate it using one method, this list is created by a user input. After it is created, I redirect from method (on my controller) to another method, sending this list, later I add it to the model to display it in a view... all perfect

So, for example

I created object myObject on methodA. from methodA I redirect to methodB sending myObject, methodB add it as modelAttribute and prints the view, lets call it viewA.

All fine until then.

What if I want, from viewA , to call a method , methodC, and methodC will have as input all the list I sent on methodB to the viewA?

At the moment I did this.

Declared the scope of the controller as session Declared a myObject as public (a variable of the class). methods A,B, C read it easily since its a public variable on the class.

What I want to do is avoid using a scope for this controller and instead have it per session have it singleton (like it is by default).

Any idea how to do this?

Upvotes: 2

Views: 1076

Answers (2)

jonasnas
jonasnas

Reputation: 3580

You shouldn't use it as a public field in controller since your controller is shared between multiple request, the state of the field won't be consistent when used by multiple users.

So either use session or pass some id of the object to ui (hidden input or cookie) and recreate the object on the next request.

Upvotes: 1

Slava Vedenin
Slava Vedenin

Reputation: 60164

You can try to use:

  1. HashMap < SessionId, List > when sessionId - is id of your user session,

  2. HashMap < String, List > when String - user name,

  3. ThreadLocal < List >, but I'm not sure that is be working correctly,

Upvotes: 0

Related Questions