You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gawain Hammond <ga...@192.com> on 2008/10/09 18:12:52 UTC

RE: Struts 2 CRUD Question

Bizzare, I'm having the exact opposite problem (my problem is the
solution you want).

Similar setup, and when I view the object's data in a form then submit
it, struts uses the exact same object instance to then submit the form.
So if I try to clear any fields (effectively null them), for a cleared
field the object keeps the old values that were originally retrieved
from persistence.

Does struts create a new object for every submit? And under what
circumstance would it not?


Cheers,
Gawain

-----Original Message-----
From: Bobby Politte [mailto:bobbypolitte@gmail.com] 
Sent: 29 September 2008 16:44
To: user@struts.apache.org
Subject: Struts 2 CRUD Question

Hi,

I'm just getting back into Struts after a few years away from web
programming.  I've got a question about methods for updating only
certain members of an object.

I've got a struts action that creates, updates and deletes a User
object. When updating though, I have two different jsp pages that
update only part of the object (user information on one, and password
on the other).  Unexpectedly (at least to me), Struts sets the members
of the User object not on the form to null.  I'd expected it to only
update the members specifically tied to controls on the form and
assume no change for any members of User *not* in the form.

Is there a way to only update those members of the object represented
on the form?  Do I *need* to use some DTO to do this?  Or split my
User object up into User and Password objects?

thanks,

Bobby

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
----------------------------------------------------------
This transmission is strictly confidential, possibly legally privileged, and intended solely for the 
addressee.  Any views or opinions expressed within it are those of the author and do not necessarily 
represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's subsidiary companies.  If you 
are not the intended recipient then you must not disclose, copy or take any action in reliance of this 
transmission. If you have received this transmission in error, please notify the sender as soon as 
possible.  No employee or agent is authorised to conclude any binding agreement on behalf of 
i-CD Publishing (UK) Ltd with another party by email without express written confirmation by an 
authorised employee of the Company. http://www.192.com (Tel: 08000 192 192).  i-CD Publishing (UK) Ltd 
is incorporated in England and Wales, company number 3148549, VAT No. GB 673128728.

Re: Struts 2 CRUD Question

Posted by Dave Newton <ne...@yahoo.com>.
--- On Fri, 10/10/08, Alberto Flores wrote:
> prototype: Scopes a single bean definition to any number of
> object instances.
> 
> request: Scopes a single bean definition to the lifecycle
> of a single HTTP request; that is each and every HTTP request 
> will have its own instance of a bean created off the back of 
> a single bean definition.  Only valid in the context of a web-aware 
> Spring ApplicationContext.
> 
> So, if the ObjectFactory uses Spring to instantiates Action
> objects, we need to have Spring instantiate these as the framework 
> was designed to (e.g. one action per request) which to me, this means
> "request" scope. Please correct me if I'm missing something.

I'm not saying "request" scope *won't* work (although I've never tried it, and requires the additional listener) but it's not required.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 CRUD Question

Posted by Alberto Flores <af...@umd.edu>.
So, from the Spring documentation:

prototype: Scopes a single bean definition to any number of object 
instances.

request: Scopes a single bean definition to the lifecycle of a single 
HTTP request; that is each and every HTTP request will have its own 
instance of a bean created off the back of a single bean definition. 
Only valid in the context of a web-aware Spring ApplicationContext.

So, if the ObjectFactory uses Spring to instantiates Action objects, we 
need to have Spring instantiate these as the framework was designed to 
(e.g. one action per request) which to me, this means "request" scope. 
Please correct me if I'm missing something.


Dave Newton wrote:
> --- On Fri, 10/10/08, Alberto Flores <af...@umd.edu> wrote:
>> Don't you mean scope="request" and not "prototype"?
> 
> No, "prototype" is the correct scope for action beans.
> 
> Dave
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 

-- 
--
Alberto
http://www.linkedin.com/in/aflores


Re: Struts 2 CRUD Question

Posted by Dave Newton <ne...@yahoo.com>.
--- On Fri, 10/10/08, Alberto Flores <af...@umd.edu> wrote:
> Don't you mean scope="request" and not "prototype"?

No, "prototype" is the correct scope for action beans.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 CRUD Question

Posted by Alberto Flores <af...@umd.edu>.
Don't you mean scope="request" and not "prototype"?

stanlick@gmail.com wrote:
> No.  Hibernate will not affect Struts's action creation.  You don't by
> chance have your action is Spring do you?  If so, make sure the scope is
> prototype or you will see the same action instance over and over.
> 
> Scott
> 
> On Fri, Oct 10, 2008 at 6:49 AM, Gawain Hammond <ga...@192.com>wrote:
> 
>> Strange,
>>
>> In the logs I can see a new Action instance being created for every
>> request. However, for a given form object getting set on the Action, I'm
>> seeing the same object instance is being set for every request. The only
>> way I can get the expected behaviour is if I shut down tomcat between
>> each request.
>>
>> I have no idea how I have got this to happen.
>>
>> As the object is persisted with hibernate, could keeping a reference to
>> the object in hibernate mean that struts would also keep a reference to
>> the object instance? Thus not creating a new object for each request?
>>
>> It's a bit of a long shot, but I'm stumped. I've not written any custom
>> interceptors or anything, it's all pretty bog standard actions and
>> struts forms.
>>
>>
>>
>> -----Original Message-----
>> From: Dave Newton [mailto:newton.dave@yahoo.com]
>> Sent: 09 October 2008 17:20
>> To: Struts Users Mailing List
>> Subject: RE: Struts 2 CRUD Question
>>
>> --- On Thu, 10/9/08, Gawain Hammond wrote:
>>> Does struts create a new object for every submit? And under
>>> what circumstance would it not?
>> Actions are created per-request, thus action variables are as well.
>> Options include retrieving a session object manually, using
>> ScopedModelDriven, a conversation interceptor, and so on.
>>
>> Dave
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>> ----------------------------------------------------------
>> This transmission is strictly confidential, possibly legally privileged,
>> and intended solely for the
>> addressee.  Any views or opinions expressed within it are those of the
>> author and do not necessarily
>> represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's
>> subsidiary companies.  If you
>> are not the intended recipient then you must not disclose, copy or take any
>> action in reliance of this
>> transmission. If you have received this transmission in error, please
>> notify the sender as soon as
>> possible.  No employee or agent is authorised to conclude any binding
>> agreement on behalf of
>> i-CD Publishing (UK) Ltd with another party by email without express
>> written confirmation by an
>> authorised employee of the Company. http://www.192.com (Tel: 08000 192
>> 192).  i-CD Publishing (UK) Ltd
>> is incorporated in England and Wales, company number 3148549, VAT No. GB
>> 673128728.
>>
> 

-- 
--
Alberto
http://www.linkedin.com/in/aflores


Re: Struts 2 CRUD Question

Posted by st...@gmail.com.
No.  Hibernate will not affect Struts's action creation.  You don't by
chance have your action is Spring do you?  If so, make sure the scope is
prototype or you will see the same action instance over and over.

Scott

On Fri, Oct 10, 2008 at 6:49 AM, Gawain Hammond <ga...@192.com>wrote:

> Strange,
>
> In the logs I can see a new Action instance being created for every
> request. However, for a given form object getting set on the Action, I'm
> seeing the same object instance is being set for every request. The only
> way I can get the expected behaviour is if I shut down tomcat between
> each request.
>
> I have no idea how I have got this to happen.
>
> As the object is persisted with hibernate, could keeping a reference to
> the object in hibernate mean that struts would also keep a reference to
> the object instance? Thus not creating a new object for each request?
>
> It's a bit of a long shot, but I'm stumped. I've not written any custom
> interceptors or anything, it's all pretty bog standard actions and
> struts forms.
>
>
>
> -----Original Message-----
> From: Dave Newton [mailto:newton.dave@yahoo.com]
> Sent: 09 October 2008 17:20
> To: Struts Users Mailing List
> Subject: RE: Struts 2 CRUD Question
>
> --- On Thu, 10/9/08, Gawain Hammond wrote:
> > Does struts create a new object for every submit? And under
> > what circumstance would it not?
>
> Actions are created per-request, thus action variables are as well.
> Options include retrieving a session object manually, using
> ScopedModelDriven, a conversation interceptor, and so on.
>
> Dave
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> ----------------------------------------------------------
> This transmission is strictly confidential, possibly legally privileged,
> and intended solely for the
> addressee.  Any views or opinions expressed within it are those of the
> author and do not necessarily
> represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's
> subsidiary companies.  If you
> are not the intended recipient then you must not disclose, copy or take any
> action in reliance of this
> transmission. If you have received this transmission in error, please
> notify the sender as soon as
> possible.  No employee or agent is authorised to conclude any binding
> agreement on behalf of
> i-CD Publishing (UK) Ltd with another party by email without express
> written confirmation by an
> authorised employee of the Company. http://www.192.com (Tel: 08000 192
> 192).  i-CD Publishing (UK) Ltd
> is incorporated in England and Wales, company number 3148549, VAT No. GB
> 673128728.
>

RE: Struts 2 CRUD Question

Posted by Gawain Hammond <ga...@192.com>.
Strange,

In the logs I can see a new Action instance being created for every
request. However, for a given form object getting set on the Action, I'm
seeing the same object instance is being set for every request. The only
way I can get the expected behaviour is if I shut down tomcat between
each request.

I have no idea how I have got this to happen.

As the object is persisted with hibernate, could keeping a reference to
the object in hibernate mean that struts would also keep a reference to
the object instance? Thus not creating a new object for each request? 

It's a bit of a long shot, but I'm stumped. I've not written any custom
interceptors or anything, it's all pretty bog standard actions and
struts forms.



-----Original Message-----
From: Dave Newton [mailto:newton.dave@yahoo.com] 
Sent: 09 October 2008 17:20
To: Struts Users Mailing List
Subject: RE: Struts 2 CRUD Question

--- On Thu, 10/9/08, Gawain Hammond wrote:
> Does struts create a new object for every submit? And under
> what circumstance would it not?

Actions are created per-request, thus action variables are as well.
Options include retrieving a session object manually, using
ScopedModelDriven, a conversation interceptor, and so on.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
----------------------------------------------------------
This transmission is strictly confidential, possibly legally privileged, and intended solely for the 
addressee.  Any views or opinions expressed within it are those of the author and do not necessarily 
represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's subsidiary companies.  If you 
are not the intended recipient then you must not disclose, copy or take any action in reliance of this 
transmission. If you have received this transmission in error, please notify the sender as soon as 
possible.  No employee or agent is authorised to conclude any binding agreement on behalf of 
i-CD Publishing (UK) Ltd with another party by email without express written confirmation by an 
authorised employee of the Company. http://www.192.com (Tel: 08000 192 192).  i-CD Publishing (UK) Ltd 
is incorporated in England and Wales, company number 3148549, VAT No. GB 673128728.

RE: Struts 2 CRUD Question

Posted by Dave Newton <ne...@yahoo.com>.
--- On Thu, 10/9/08, Gawain Hammond wrote:
> Does struts create a new object for every submit? And under
> what circumstance would it not?

Actions are created per-request, thus action variables are as well. Options include retrieving a session object manually, using ScopedModelDriven, a conversation interceptor, and so on.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org