You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Caroline Lauferon <ca...@cgey.com> on 2003/11/19 09:55:14 UTC

chained actions

I don't understand what is happening: I am chaining to Actions that use the
same form input. when i submit the form, Action1 is called, and the form
parameter is well populated. at the end of action1, I make a forward to
Action2. and in Action2, the form is emptied!!???
I don't understand what is happening, and why. the solution I have found is
not to specify a form bean for action2 and get it by
request.getAttribute("MyForm"). i t works, but I would like to understand
why struts creates a new form bean, overriding the existing one.
I hope someone will explain me.

Caroline
PS: here is a snippet of struts-config.xml, Action1.java and Action2.java

<action path="/action1" type="package.Action1" name="MyForm"
scope="request">
   <forward name="ok" path="/action2.do"/>
</action>
 <action path="/action2" type="package.Action2"  name="MyForm"
scope="request"> >
   <forward name="ok" path="nextpage"/>
</action>

/// Action1
MyForm f = (MyForm ) a_form;
s_logger.info("f.getField1() : "+ f.getField1()); // outputs a value

/// Action2
MyForm f = (MyForm ) a_form;
s_logger.info("f.getField1() : "+ f.getField1()); // outputs null


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


Re: chained actions

Posted by Nicolas De Loof <ni...@cgey.com>.
Here is what happens :

reset() is called before every form population
form population occurs before every struts action url (*.do) is processed.

When an action forwards to another one using <forward path="another.do">, struts actionServlet is called to handle this
second URL. FormBean is then reseted and populated from request.

If you doesn't use a redirect, request is the same an all parameters are still in it, so formbean is populated the same
way for the two actions.

If you use a redirect, when first action forwards, a HTTP Redirect response (with location="another.do") is sent to the
browser. Browser follows new location and builds a new request, but this new request hasn't any parameter. When form
population occurs for "another.do", form-bean is reseted and population has no effect (no parameters !).

Nico.


----- Original Message ----- 
From: "Caroline Lauferon" <ca...@cgey.com>
To: <an...@gridnode.com>; "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Wednesday, November 19, 2003 11:31 AM
Subject: Re: chained actions


> > Is it a multipart form (file upload)?
> yes..... and since you asked me, I just changed it..... and now it works???
> (but the reset method is called between the two action in both cases). I
> don't need this encoding, but I wanted to always use the same skeleton,
> that's why i used it.
> can you explain me what has been happening?
>
>
> Caroline, puzzled
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org


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


Re: chained actions

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
On 11/19/2003 12:13 PM Caroline Lauferon wrote:
> Wahouh! thanks a lot for your great explanation!
> thanks to all those who have also helped me: Adam Hardy and Nicolas De Loof
> ;-)

and thanks Andrew from me too - I never knew that.


Adam
-- 
struts 1.1 + tomcat 5.0.12 + java 1.4.2
Linux 2.4.20 RH9

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


Tokens

Posted by Gurpreet Dhanoa <gu...@smartdatainc.com>.
hi All

 Can somebody explain small code snap shot of using Tokens. AS i am trying
to use it in one of my application. but due to some unknown reason it is not
working. I may be doing something wrong.


Any help will be appreciated


Regards
GAry


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


Re: chained actions

Posted by Caroline Lauferon <ca...@cgey.com>.
Wahouh! thanks a lot for your great explanation!
thanks to all those who have also helped me: Adam Hardy and Nicolas De Loof
;-)

Caroline




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


RE: chained actions

Posted by Andrew Hill <an...@gridnode.com>.
Ahhh.
Multipart requests are funny beasts. They dont work the same way as normal
requests.

In a normal request the servlet container will parse the request and
initialise the request parameters in the request object (which you can then
obtain through such calls as request.getParameter("bob")....).

Multipart requests however are submitted a different way to normal requests.
The servlet container is too "stuck up" to associate with such working-class
request types (something to do with the format being merely an RFC and not
an official standard or some such bourgeious nonsense) and so for a
multipart request, getParameter() will return null!!!

This is where struts comes in. Struts is much nicer than your average
servlet container, and it provides special support for multipart requests to
help make them act like normal requests for most purposes. (Well actually I
think its in some commons project now that struts leverages off - but the
origin of this code was actually in struts so I'll just lump it all in
together as "Struts" for the purposes of this discussion).

When a multipart request is submitted the request parameters arent
initialised by your servlet container. Struts will create a RequestWrapper
object to wrap the request and it will read from the requests input stream
and initialise the parameters in this RequestWrapper object. (It will also
read the files being uploaded and create the FormFile objects for the file
fields if there are any). The request object that gets passed to your action
is thus in fact this Multipart request wrapper object created by struts.
(You can verify this by comparing the results of a
request.getClass().getName() call for a multipart and non-multipart request
sent to your action).

When the action has finished processing the request processor will "unwrap"
the request wrapper it created and forward based on the original request
object.

Now what I think is happening to you here (and Im not 100% sure) is that
when the second action is hit, the input stream for the request has been
read already and cannot be re-read - and thus the multipart request cant be
processed a second time. Your best bet for a workaround is to do something
like what you were doing before - store a reference to the actionform
populated for the first request and look this up in the second.

hth
Andrew



-----Original Message-----
From: Caroline Lauferon [mailto:caroline.lauferon@cgey.com]
Sent: Wednesday, 19 November 2003 18:32
To: andrew.david.hill@gridnode.com; Struts Users Mailing List
Subject: Re: chained actions


> Is it a multipart form (file upload)?
yes..... and since you asked me, I just changed it..... and now it works???
(but the reset method is called between the two action in both cases). I
don't need this encoding, but I wanted to always use the same skeleton,
that's why i used it.
can you explain me what has been happening?


Caroline, puzzled


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


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


Re: chained actions

Posted by Caroline Lauferon <ca...@cgey.com>.
> Is it a multipart form (file upload)?
yes..... and since you asked me, I just changed it..... and now it works???
(but the reset method is called between the two action in both cases). I
don't need this encoding, but I wanted to always use the same skeleton,
that's why i used it.
can you explain me what has been happening?


Caroline, puzzled


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


RE: chained actions

Posted by Andrew Hill <an...@gridnode.com>.
<snip>
I'm not sure I understand what you mean.... do you mean the form fields by
parameters?
</snip>

Yes. Thats what I mean. In this case you say you arent modifying any of the
form properties but just forwarding straight to the next action. This is
quite mysterious actually. Based on all the info you have given it should be
working! Hmmm.

Is it a multipart form (file upload)?

Definately no exceptions/errors from struts in the log?

Can you dump the request parameters in the second action to make sure the
parameters are there?
Also try putting a logging statement in the forms reset() method to see if
its being called again for the second action.



-----Original Message-----
From: Caroline Lauferon [mailto:caroline.lauferon@cgey.com]
Sent: Wednesday, 19 November 2003 17:33
To: Struts Users Mailing List; andrew.david.hill@gridnode.com
Subject: Re: chained actions


> My guess it that the parameters in question are being set in the first
> action and there isnt actually a corresponding parameter value in the
> request and they are then cleared by reset() prior toi hitting action 2.
Is
> this the case Caroline?

I'm not sure I understand what you mean.... do you mean the form fields by
parameters?
If so, that's not what i am doing. My form has several submission buttons.
Action1 is just logging the field values and forwarding to the action
corresponding to the button used to submit. No modification is made.

i found in the archives: "when the second action is being invoked, the form
is being 'repopulated' with the initial form values that the form had when
it was passed to the first action ". and that's not what is happening :-(

Caroline



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


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


Re: chained actions

Posted by Caroline Lauferon <ca...@cgey.com>.
> My guess it that the parameters in question are being set in the first
> action and there isnt actually a corresponding parameter value in the
> request and they are then cleared by reset() prior toi hitting action 2.
Is
> this the case Caroline?

I'm not sure I understand what you mean.... do you mean the form fields by
parameters?
If so, that's not what i am doing. My form has several submission buttons.
Action1 is just logging the field values and forwarding to the action
corresponding to the button used to submit. No modification is made.

i found in the archives: "when the second action is being invoked, the form
is being 'repopulated' with the initial form values that the form had when
it was passed to the first action ". and that's not what is happening :-(

Caroline



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


RE: chained actions

Posted by Andrew Hill <an...@gridnode.com>.
Cant be redirecting though. If it was then one would not be able to retrieve
the form object from the request attributes in the second action.
My guess it that the parameters in question are being set in the first
action and there isnt actually a corresponding parameter value in the
request and they are then cleared by reset() prior toi hitting action 2. Is
this the case Caroline?

<snip>
PS Andrew - limited action chaining isn't that bad. Are you claiming you
don't do any at all?
</snip>

Ah.. ahem, well I....
Im only human, and sometimes ... well you know...  but its not something Im
proud of ok.
<blush/>

-----Original Message-----
From: Adam Hardy [mailto:ahardy.struts@cyberspaceroad.com]
Sent: Wednesday, 19 November 2003 17:11
To: Struts Users Mailing List
Subject: Re: chained actions


Hi Caroline,

the only reason that I know of which would explain the loss of the form
property values is redirecting from action1 to action2, but your action
mappings don't show redirect="true".

Are you redirecting by default somehow? I know it is possible for
instance with an init-param on your action servlet config in web.xml.

Adam
PS Andrew - limited action chaining isn't that bad. Are you claiming you
don't do any at all?


On 11/19/2003 09:55 AM Caroline Lauferon wrote:
> I don't understand what is happening: I am chaining to Actions that use
the
> same form input. when i submit the form, Action1 is called, and the form
> parameter is well populated. at the end of action1, I make a forward to
> Action2. and in Action2, the form is emptied!!???
> I don't understand what is happening, and why. the solution I have found
is
> not to specify a form bean for action2 and get it by
> request.getAttribute("MyForm"). i t works, but I would like to understand
> why struts creates a new form bean, overriding the existing one.
> I hope someone will explain me.
>
> Caroline
> PS: here is a snippet of struts-config.xml, Action1.java and Action2.java
>
> <action path="/action1" type="package.Action1" name="MyForm"
> scope="request">
>    <forward name="ok" path="/action2.do"/>
> </action>
>  <action path="/action2" type="package.Action2"  name="MyForm"
> scope="request"> >
>    <forward name="ok" path="nextpage"/>
> </action>
>
> /// Action1
> MyForm f = (MyForm ) a_form;
> s_logger.info("f.getField1() : "+ f.getField1()); // outputs a value
>
> /// Action2
> MyForm f = (MyForm ) a_form;
> s_logger.info("f.getField1() : "+ f.getField1()); // outputs null


--
struts 1.1 + tomcat 5.0.12 + java 1.4.2
Linux 2.4.20 RH9

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


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


Re: chained actions

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
Hi Caroline,

the only reason that I know of which would explain the loss of the form 
property values is redirecting from action1 to action2, but your action 
mappings don't show redirect="true".

Are you redirecting by default somehow? I know it is possible for 
instance with an init-param on your action servlet config in web.xml.

Adam
PS Andrew - limited action chaining isn't that bad. Are you claiming you 
don't do any at all?


On 11/19/2003 09:55 AM Caroline Lauferon wrote:
> I don't understand what is happening: I am chaining to Actions that use the
> same form input. when i submit the form, Action1 is called, and the form
> parameter is well populated. at the end of action1, I make a forward to
> Action2. and in Action2, the form is emptied!!???
> I don't understand what is happening, and why. the solution I have found is
> not to specify a form bean for action2 and get it by
> request.getAttribute("MyForm"). i t works, but I would like to understand
> why struts creates a new form bean, overriding the existing one.
> I hope someone will explain me.
> 
> Caroline
> PS: here is a snippet of struts-config.xml, Action1.java and Action2.java
> 
> <action path="/action1" type="package.Action1" name="MyForm"
> scope="request">
>    <forward name="ok" path="/action2.do"/>
> </action>
>  <action path="/action2" type="package.Action2"  name="MyForm"
> scope="request"> >
>    <forward name="ok" path="nextpage"/>
> </action>
> 
> /// Action1
> MyForm f = (MyForm ) a_form;
> s_logger.info("f.getField1() : "+ f.getField1()); // outputs a value
> 
> /// Action2
> MyForm f = (MyForm ) a_form;
> s_logger.info("f.getField1() : "+ f.getField1()); // outputs null


-- 
struts 1.1 + tomcat 5.0.12 + java 1.4.2
Linux 2.4.20 RH9

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


RE: chained actions

Posted by Andrew Hill <an...@gridnode.com>.
Action chaining is considered bad practice.
There are numerous threads discussing the reasons - search the archive for
details.

Remember of course that when you hit the second action the RequestProcessor
executes again - this means that reset() is called again - and the form
repopulated - which makes your symptoms "emptied" kind of strange - one
would expect to see the form repopulated from the request (with changes you
made to its contents in the first action being written over) but not emptied
(unless none of those properties were in the request but were cleared in
reset). Are you using a redirecting forward? hmmm. No you cant be if you can
still find the form in the request.

Anyhow, these are just the tip of the iceberg when it comes to the sort of
fun problems you get from commiting the sin of action chaining.


-----Original Message-----
From: Caroline Lauferon [mailto:caroline.lauferon@cgey.com]
Sent: Wednesday, 19 November 2003 16:55
To: Struts Users Mailing List
Subject: chained actions


I don't understand what is happening: I am chaining to Actions that use the
same form input. when i submit the form, Action1 is called, and the form
parameter is well populated. at the end of action1, I make a forward to
Action2. and in Action2, the form is emptied!!???
I don't understand what is happening, and why. the solution I have found is
not to specify a form bean for action2 and get it by
request.getAttribute("MyForm"). i t works, but I would like to understand
why struts creates a new form bean, overriding the existing one.
I hope someone will explain me.

Caroline
PS: here is a snippet of struts-config.xml, Action1.java and Action2.java

<action path="/action1" type="package.Action1" name="MyForm"
scope="request">
   <forward name="ok" path="/action2.do"/>
</action>
 <action path="/action2" type="package.Action2"  name="MyForm"
scope="request"> >
   <forward name="ok" path="nextpage"/>
</action>

/// Action1
MyForm f = (MyForm ) a_form;
s_logger.info("f.getField1() : "+ f.getField1()); // outputs a value

/// Action2
MyForm f = (MyForm ) a_form;
s_logger.info("f.getField1() : "+ f.getField1()); // outputs null


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


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