wood burning stoves 2.0*
The moose likes Struts and the fly likes Passing ArrayList from action to action? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing ArrayList from action to action?" Watch "Passing ArrayList from action to action?" New topic
Author

Passing ArrayList from action to action?

Bianca Deloso
Greenhorn

Joined: Sep 23, 2003
Posts: 15
Hi guys,
Is it possible to pass an ArrayList from action to action via a session? If so, can somebody tell me how and if possible, post a sample code?
I want to be able to add to this ArrayList everytime I perform a certain action (like adding a new line), and if I click on another button, the ones in the ArrayList will be added to the DB.
Is this a way to get the arrayList (from a different action)? (see below)
session.getAttribute("nameOfArrayList")
Thanks!
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Hi,
Yes, you can.
1. In each action that needs the ArrayList.


-Yoo-Jin
[ October 28, 2003: Message edited by: Yoo-Jin Lee ]
Bianca Deloso
Greenhorn

Joined: Sep 23, 2003
Posts: 15
Hi!
Could you guys check if this is correct:
ConfigItem configItems = (ConfigItem) session.getAttribute("configItems");
I have a "bean" called ConfigItem (which has the getter and setters) and I want to be able to use certain values in the arrayList. Is the above line correct? (assuming I have a configItems ArrayList that is in the session already.
Thanks again!
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
This should throw a class cast item at run time. What you are trying to do here is pull an object of type ArrayList out of the session and then cast it to an object of type ActionForm (ConfigItem in your case). This will not work.
What do you want to achieve?
Sahil


Adios
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
This should throw a ClassCastException at run time. What you are trying to do here is pull an object of type ArrayList out of the session and then cast it to an object of type ActionForm (ConfigItem in your case). This will not work.
What do you want to achieve?
Sahil
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
This should throw a ClassCastException at run time. What you are trying to do here is pull an object of type ArrayList out of the session and then cast it to an object of type ActionForm (ConfigItem in your case). This will not work.
What do you want to achieve?
Sahil
Bianca Deloso
Greenhorn

Joined: Sep 23, 2003
Posts: 15
Hi!
I actually wasn't sure how to get the 'separate' values in the ArrayList I created (with the use of a bean) so I could insert it into a db.
The ArrayList I'm retrieving is from the session that has 'bean' entries. I was wondering how I could access this. I was at first thinking of something like this:
configItem.getFirstName
configItem.getLastName
...etc...
so I could use these values in adding to the db. Anyway I could do this with an ArrayList?
Thanks!
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Hi Bianca,
I think I would encapsulate each 'action' as a bean and then save these beans in a list.
If I understand correctly you could do:
// to set list
List configItems = (List) session.getAttribute("configItems");
if (configItems == null) {
configItems = new LinkedList();
session.setAttribute("configItems", configItems);
}
ConfigItem action = new ConfigItem();
// populate the action or else use an actionform... etc..
configItems.add(action);

// to save the list
List configItems = (List) session.getAttribute("configItems");
Iterator it = list.iterator();
ConfigItem item;
while (it.hasNext()) {
item = (ConfigItem ) it.next();
// save the item in the database
}
Does this make things clearer?
-Yoo-Jin
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Passing ArrayList from action to action?
 
Similar Threads
ArrayList and Ajax
arraylist nullified in logic:notempty
Collection on jsp
ArrayLists and Submit
dynamic multibox - populate an Array in ActionForm from Action class