You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Howard Miller <me...@howardmiller.co.uk> on 2002/10/03 00:18:15 UTC

Copying beans

Hi,

This might be more of a java question, but please stick with me.

I have a session scope bean which represents my user's configuration. In the bean is 
an ArrayList which contains a list of items that the user "owns". Each item in the list is 
itself a bean containing a number of fields. This lot is loaded from a database when 
the user logs in.

Two questions....

If I want to edit one of the beans in the list, do I have to copy one of the beans in the 
list into a "standalone" form-bean, and then copy it back into the "list-bean" after the 
user has edited it. OR is there some way that I can use the bean as a form-bean (ie, 
in a struts jsp page) directly? Copying all the data seems like a waste of time.

Is there any *reliable* way of catching when a session scope object is going to be 
destroyed so that I can save it to the database, or am I better doing it everytime it is 
updated?

Thanks

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Copying beans

Posted by Steve Vanspall <st...@crmsoftware.com.au>.
Not sure if you have had a reply yet.

I have a few ArrayLists with beans in them.

Unfortunately, I have not found a way of just accessing one of them.

However if you jst pull it out of the ArrayList and place it in a field, say
thisBean or something like that, you can edit it.

If you just place the same object into the field, than the changes will be
reflected in the ArrayList.

i.e. setThisBean((MyBean) list.get(0))

then any changes will be reflected in the bean at index 0 of the ArrayList,
there is no need to actually place it back in the ArrayList as that object
is already there. This won't, however, work for an ArrayList of Strings or
any primitive datatypes.

As for the second question, I am not sure. Usually you can tell in the code
whether or not anything is going to be still holding on the the bean.

What's an example of where the bean may have been destroyed, that you need
to test for?

Regards

Steve

----- Original Message -----
From: "Howard Miller" <me...@howardmiller.co.uk>
To: <st...@jakarta.apache.org>
Sent: Thursday, October 03, 2002 8:18 AM
Subject: Copying beans


> Hi,
>
> This might be more of a java question, but please stick with me.
>
> I have a session scope bean which represents my user's configuration. In
the bean is
> an ArrayList which contains a list of items that the user "owns". Each
item in the list is
> itself a bean containing a number of fields. This lot is loaded from a
database when
> the user logs in.
>
> Two questions....
>
> If I want to edit one of the beans in the list, do I have to copy one of
the beans in the
> list into a "standalone" form-bean, and then copy it back into the
"list-bean" after the
> user has edited it. OR is there some way that I can use the bean as a
form-bean (ie,
> in a struts jsp page) directly? Copying all the data seems like a waste of
time.
>
> Is there any *reliable* way of catching when a session scope object is
going to be
> destroyed so that I can save it to the database, or am I better doing it
everytime it is
> updated?
>
> Thanks
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>