File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Passing ArrayList of objects between action classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing ArrayList of objects between action classes" Watch "Passing ArrayList of objects between action classes" New topic
Author

Passing ArrayList of objects between action classes

Michael Brown
Greenhorn

Joined: May 05, 2008
Posts: 27
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?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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.

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Michael Brown
Greenhorn

Joined: May 05, 2008
Posts: 27
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: May 05, 2008
Posts: 27
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...
 
 
subject: Passing ArrayList of objects between action classes