• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Passing ArrayList of objects between action classes

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

can someone , please, help me with this:

I have an Action Class Action1 that has a property "MyList" that is an ArrayList<MyObject> . MyObject is a custom object that has quite number of properties.
I need to "transfer" this List from my Action1 over it's view ( for example "action.jsp" ) to some other ActionClass , for example Action2.

I have no problems passing primitive variables, of course , but how do I set something in the JSP page to "hold" information about object and to pass it to next action class when submit.

For the moment, I do this by setting a session variable "list" that is a copy of "MyList" and just use it in the next ActionClass by taking it from the session.
But this is not a good solution because if user decide to open several tabs in order to do several things in the application at the same moment, he would easily get his session variable "list" overriden by some other Action class in some other tab in the broswer...

What is the best way ( or ways ) to do this?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Brown wrote:For the moment, I do this by setting a session variable "list" that is a copy of "MyList" and just use it in the next ActionClass by taking it from the session.
But this is not a good solution because if user decide to open several tabs in order to do several things in the application at the same moment, he would easily get his session variable "list" overriden by some other Action class in some other tab in the broswer...


You can always avoid creating multiple instance of same list in user session by just checking the variable existence in the current session. If the list is already set in the session then don't reset it. You have to put that conditional code in one of the action.

And for this kind of collections object, the session way is correct and straightforward solution.
 
Michael Brown
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sagar Rohankar, thanks a lot for your time and answer!

I know that this is one of the good solutions, but here is the thing:

In my application I use my custom ActionSupport class "MyActionSupport" that extends ActionSupport and allways uses this list ( currently from session ).
MyActionSupport almost always needs this list in order to manipulate some DB data properly.

And every ActionClass in my application that is of type "MyActionSupport" uses different instance of "MyList".
Sometimes it is a list of 4 elements , sometimes it's a list of 10 elements, and so on...
It depends which DB table I am processing with it...

So, If a user opens a tab in the browser to work with employees table, for example, he is going to need a list of 10 objects, each with it's properties set to some values.
At the same time, in another tab of the browser, he opens an application and work around with sales table, and for that MyActionSupport class uses a list of , for example, 20 objects, each with it's properties set to some other values than the ones in list for employees...

So, we have two different lists that needs to be set in the session or somewhere else.

At this moment, on execute() , MyActionSupport allways rewrites the current list in the session and sets it's value with the one in itself.

So, this is a problem...
I hope I explained it a little better...
Is there some other solution for passing a list of objects between two action classes ove a view ( some .jsp page )?

Hmmm... I already see myself putting this list in a map ( HashMap<String,ArrayList><MyObject>> , where the key is the name of the instance of the action that set the list, and the value is the list that this action contains... ) so that every action class uses it's own list...
In your opinion, would this be a good solution...?

 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like your using Struts 2, if its so, then pardon me, I'll be of littile use.

Hmmm... I already see myself putting this list in a map ( HashMap<String,ArrayList><MyObject>> , where the key is the name of the instance of the action that set the list, and the value is the list that this action contains... ) so that every action class uses it's own list...
In your opinion, would this be a good solution...?


Yeah, that's look like a good solution, but you can wait till some more S2 experienced Rancher explain little better for us ;)
 
Michael Brown
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is Struts2...
Anyway, thanks for your answer.

Consider this done.
I'll make a map in a session, and assign a list to it's concrete MyActionSupport so that every instance of this class overwrites only it's list...
 
reply
    Bookmark Topic Watch Topic
  • New Topic