• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing ArrayList from action to action?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sandy gupta
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic