You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Marcio Ghiraldelli <ma...@neobiz.com.br> on 2006/03/22 14:46:12 UTC

Strange Form Reset Behavior

    Hello,

    I am facing a strange ActionForm Reset behavior with Struts 1.2:

    I want after submit a form, redirect via controler to the same form (so the user can submit it subsequently times).
    Should the normal behavior be:

    1) Controler receives the first request (Action.do)
    2) My Action class populate the request properly
    3) Controler redirects to the form view
    4) View calls an ActionForm (request scope)
    5) ActionForm Reset method is executed, accessing the request properly
    6) User submits the Form (ActionSubmit.do)
    7) Controler redirects to ActionSubmit.class
    8) ActionSubmit works ok, and return a Forward to recall the Action.do

    The steps above should implement a loop of subsequently submits (for adding itens in a list by example) but after the first submit, in the second step, the reset method is executed BEFORE the Action.do, with an empty request, breaking down the view proccess, 'couse the form isn't populated correctly.

    What's happening here?

Re: Strange Form Reset Behavior

Posted by Michael Jouravlev <jm...@gmail.com>.
You should clean the form only on input phase. Thus, you need to
somehow distinguish input phase from render phase since reset() does
not make this distinction. If you always submit a form using POST, you
can assume that POST means input phase. Or, you can check for a
certain parameter in the request (event) and if it is there, you can
assume that this is input phase.

Read here for some insights:
http://struts.sourceforge.net/strutsdialogs/dialogaction.html

If you can switch to 1.2.9, it contains EventActionDispatcher that
makes DialogAction obsolete. See also here:

http://wiki.apache.org/struts/DataEntryForm
http://wiki.apache.org/struts/EventActionDispatcher

Remember, you task is:

* to distinguish between input phase and render phase
* to ensure that request corresponding to render phase contained no
paramaters, otherwise you would have to check current phase in every
setter on your form.

This is all needed if you want to use redirect. If you simply ok with
forwarding to the same page after adding a row, than above techniques
are not needed.

Michael.

On 3/22/06, Marcio Ghiraldelli <ma...@neobiz.com.br> wrote:
>     Hello,
>
>     I am facing a strange ActionForm Reset behavior with Struts 1.2:
>
>     I want after submit a form, redirect via controler to the same form (so the user can submit it subsequently times).
>     Should the normal behavior be:
>
>     1) Controler receives the first request (Action.do)
>     2) My Action class populate the request properly
>     3) Controler redirects to the form view
>     4) View calls an ActionForm (request scope)
>     5) ActionForm Reset method is executed, accessing the request properly
>     6) User submits the Form (ActionSubmit.do)
>     7) Controler redirects to ActionSubmit.class
>     8) ActionSubmit works ok, and return a Forward to recall the Action.do
>
>     The steps above should implement a loop of subsequently submits (for adding itens in a list by example) but after the first submit, in the second step, the reset method is executed BEFORE the Action.do, with an empty request, breaking down the view proccess, 'couse the form isn't populated correctly.
>
>     What's happening here?
>

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


Re: Strange Form Reset Behavior

Posted by Marcio Ghiraldelli <ma...@neobiz.com.br>.
    Thanks to the newbie Struts 1.2 FAQ, I realized what I was doing wrong. 
Now I'd understood how to prepopulate the ActionForm in the Action, and use 
Reset method only for View independent initialisation.

    http://struts.apache.org/struts-doc-1.2.x/faqs/newbie.html#prepopulate

    Cool


>        <action path="/FirstAction" type="action.First">
>            <forward name="success" path="/form.jsp" />
>            name="Form"
>            scope="request"
>        </action>
>
>        <action path="/FirstActionSubmit" type="action.FirstSubmit"
>            name="Form"
>            scope="request"
>            input="/form.jsp"
>            validate="true"
>        >


----- Original Message ----- 
From: "Marcio Ghiraldelli" <ma...@neobiz.com.br>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Thursday, March 23, 2006 11:05 AM
Subject: Re: Strange Form Reset Behavior


>
>    In the example below, the "FirstAction" action is responsable for 
> getting a list from the db and pass it to the form "Form".
>
>    During the execution of the "action.First" class, doesn't exists yet 
> any instance of the form. When the "action.First" class redirects to the 
> "form.jsp", after loading the refered list, there exists an <htm:form 
> action="/FirstActionSubmit"> and here the Struts creates a new instance of 
> the form.
>
>    So, how can I pass the list loaded in the "FirstAction" to the "Form", 
> without using request?
>    The form instance is only created in the view!
>
>    - puzzled! -
>
>        <form-bean name="Form" type="forms.Form" />
>
>        <action path="/FirstAction" type="action.First">
>            <forward name="success" path="/form.jsp" />
>        </action>
>
>        <action path="/FirstActionSubmit" type="action.FirstSubmit"
>            name="Form"
>            scope="request"
>            input="/form.jsp"
>            validate="true"
>        > 


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


Re: Strange Form Reset Behavior

Posted by Marcio Ghiraldelli <ma...@neobiz.com.br>.
    In the example below, the "FirstAction" action is responsable for 
getting a list from the db and pass it to the form "Form".

    During the execution of the "action.First" class, doesn't exists yet any 
instance of the form. When the "action.First" class redirects to the 
"form.jsp", after loading the refered list, there exists an <htm:form 
action="/FirstActionSubmit"> and here the Struts creates a new instance of 
the form.

    So, how can I pass the list loaded in the "FirstAction" to the "Form", 
without using request?
    The form instance is only created in the view!

    - puzzled! -

        <form-bean name="Form" type="forms.Form" />

        <action path="/FirstAction" type="action.First">
            <forward name="success" path="/form.jsp" />
        </action>

        <action path="/FirstActionSubmit" type="action.FirstSubmit"
            name="Form"
            scope="request"
            input="/form.jsp"
            validate="true"
        >

----- Original Message ----- 
From: "Dave Newton" <ne...@pingsite.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Thursday, March 23, 2006 10:13 AM
Subject: Re: Strange Form Reset Behavior


> Marcio Ghiraldelli wrote:
>> How? Supose I have a form with an select combo. The itens should be
>> populated with a database result. The only way I could acomplish this
>> is getting data on the Action before the form view, set the list in
>> the request, redirect to the view. Then, the view creates an instance
>> of the form, the reset is called and them it populates the data via
>> the request.
>>
>>    How should I populate the form BEFORE Struts reaches the view, as
>> doens't exists an form instance before it??
>
> The view doesn't create an instance of the form, the Action does, and
> passes it to the view.
>
> For instance, I have often used a simple subclass of Action as a base
> class for all my actions that separates the execute method into
> executeGet and executePost methods. This methods might share some
> functionality in a certain Action, like populating a list for a drop-down.
>
> Dave
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> 


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


Re: Strange Form Reset Behavior

Posted by Dave Newton <ne...@pingsite.com>.
Marcio Ghiraldelli wrote:
> How? Supose I have a form with an select combo. The itens should be
> populated with a database result. The only way I could acomplish this
> is getting data on the Action before the form view, set the list in
> the request, redirect to the view. Then, the view creates an instance
> of the form, the reset is called and them it populates the data via
> the request.
>
>    How should I populate the form BEFORE Struts reaches the view, as
> doens't exists an form instance before it??

The view doesn't create an instance of the form, the Action does, and
passes it to the view.

For instance, I have often used a simple subclass of Action as a base
class for all my actions that separates the execute method into
executeGet and executePost methods. This methods might share some
functionality in a certain Action, like populating a list for a drop-down.

Dave



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


Re: Strange Form Reset Behavior

Posted by Marcio Ghiraldelli <ma...@neobiz.com.br>.
> If you want to just return to the same page after you submit, then simply 
> provide that as the return value in your Action and have it mapped just 
> like you would for going to any other page in your flow. There is no 
> difference what so ever. Maybe I'm confused what you are trying to 
> accomplish?

    No, you're not confused. This is exatcly what I want, and what I tough. 
Simple like that.

> You should be populating your form in the Action, not the reset.

    How? Supose I have a form with an select combo. The itens should be 
populated with a database result. The only way I could acomplish this is 
getting data on the Action before the form view, set the list in the 
request, redirect to the view. Then, the view creates an instance of the 
form, the reset is called and them it populates the data via the request.

    How should I populate the form BEFORE Struts reaches the view, as 
doens't exists an form instance before it??


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


Re: Strange Form Reset Behavior

Posted by Rick Reumann <st...@reumann.net>.
Marcio Ghiraldelli wrote the following on 3/22/2006 8:46 AM:
>     Hello,
>
> I am facing a strange ActionForm Reset behavior with Struts 1.2:
> 
> I want after submit a form, redirect via controler to the same form
> (so the user can submit it subsequently times).


If you want to just return to the same page after you submit, then 
simply provide that as the return value in your Action and have it 
mapped just like you would for going to any other page in your flow. 
There is no difference what so ever. Maybe I'm confused what you are 
trying to accomplish?

> Should the normal behavior be:
>     1) Controler receives the first request (Action.do)
>     2) My Action class populate the request properly
>     3) Controler redirects to the form view
>     4) View calls an ActionForm (request scope)
>     5) ActionForm Reset method is executed, accessing the request properly
>     6) User submits the Form (ActionSubmit.do)
>     7) Controler redirects to ActionSubmit.class
>     8) ActionSubmit works ok, and return a Forward to recall the Action.do

I didn't read all the steps you have above although some steps are 
definitely wrong. The reset method is always called on every action 
request. But what are you trying to do in your reset method that would 
mess with stuff? Typically you only need to use the reset for setting 
booleans to false, and/or possibly resetting the size of some nested 
Lists in your ActionForm.
> 

> The steps above should implement a loop of subsequently submits (for
> adding itens in a list by example) but after the first submit, in the
> second step, the reset method is executed BEFORE the Action.do, with
> an empty request, breaking down the view proccess, 'couse the form
> isn't populated correctly.

You should be populating your form in the Action, not the reset.


-- 
Rick
http://www.learntechnology.net

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