You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Adam Lipscombe <ad...@expensys.com> on 2005/02/04 17:45:45 UTC

Best practice for formbean scope and handling form validation errors

Folks,


I have 2 actions which look (sort of) like this:

<action name="myActionForm" path="/initAction" scope="request"
type="com.xxx.yyy.myAction" validate="false" >
   <forward name="displayForm" path="/Form1.jsp" />
</action>

<action input="/Form1.jsp" name="myActionForm" path="/processAction"
scope="request" validate="true" >
  <forward name="nextStep" path="/Form2.jsp" />
</action> 

If the Form1.jsp is submitted the "/processAction.do" gets called.
Validation occurs in either the "myActionForm" actionform or the "myAction"
action.

If the validation fails a struts ActionErrors object is populated and
Form1.jsp is re-displayed by getting the forward like this:

 forward = actionMapping.getInputForward();





But, if the scope of the "myActionForm" formbean is "request" a new instance
of the formbean is constructed when Form1.jsp is re-displayed. Hence none of
the data that it originally held in it is present and the form fails. The
formBean was originally populated by /initAction.do
In order to preserve the myAction formBean I have to make its scope
"session".

I need to re-display the Form1.jsp if validation fails so that it shows the
correct formBean values as populated by /initAction.do. However it seems
that eventually I will have a cluttered session object with many formBeans
that I don't need immediately.


Is this normal? 
Or is there a better way to do it where one can have formBeans in "request"
scope and still preserve their contents if validation fails?
What is best practice?


TIA -Adam


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


Re: Best practice for formbean scope and handling form validation errors

Posted by Jeff Beal <jb...@webmedx.com>.
Adam Lipscombe wrote:

> In this instance the problem is this:
> 
> Form1 has a logic:iterate tag that iterates over a List held in
> myActionForm.
> The List is setup by the initAction.do action.
> 
> How do I preserve the List between actionform instances?
> (It's not referenced in the JSP other than the logic:iterate tag)

Try changing the input attribute to point to the /initAction.do instead 
of the JSP page.  I think that will work.

-- Jeff



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


RE: Best practice for formbean scope and handling form validation errors

Posted by Adam Lipscombe <ad...@expensys.com>.
Thanks Jeff, your analysis is spot on.


In this instance the problem is this:

Form1 has a logic:iterate tag that iterates over a List held in
myActionForm.
The List is setup by the initAction.do action.

How do I preserve the List between actionform instances?
(It's not referenced in the JSP other than the logic:iterate tag)


Tia -Adam


-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Jeff Beal
Sent: 04 February 2005 17:14
To: user@struts.apache.org
Subject: Re: Best practice for formbean scope and handling form validation
errors


Adam Lipscombe wrote:
<snip>
> But, if the scope of the "myActionForm" formbean is "request" a new 
> instance of the formbean is constructed when Form1.jsp is 
> re-displayed. Hence none of the data that it originally held in it is 
> present and the form fails. The formBean was originally populated by 
> /initAction.do In order to preserve the myAction formBean I have to 
> make its scope "session".
</snip>

Based on the rest of your email, this is what should be happening:

  1) initAction.do is populating myActionForm with data from your Model 
layer
  2) Form1.jsp pulls the values out of myActionForm and displays them to 
the user
  3) The request ends, and the myActionForm instance created in (1) is 
destroyed
  4) The user makes changes to the form values on Form1.jsp and submits 
the changes to the server
  5) Struts creates a new instance of myActionForm and populates it with 
values from the request. *If Form1.jsp was written correctly, all of the 
values populated from the Model in (1) along with any user changes from 
(4) should be in the request*
  6) myActionForm is validated.  If the validation fails, control is 
returned to (3) with the data from (4)

I'm a little unclear as to where exactly your problem is, but it seems 
that some or all of the values from the original myActionForm are not 
getting written to the user, and therefore are not being submitted with 
the second request to populate the second instance of myActionForm.

-- Jeff


---------------------------------------------------------------------
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: Best practice for formbean scope and handling form validation errors

Posted by Jeff Beal <jb...@webmedx.com>.
Adam Lipscombe wrote:
<snip>
> But, if the scope of the "myActionForm" formbean is "request" a new instance
> of the formbean is constructed when Form1.jsp is re-displayed. Hence none of
> the data that it originally held in it is present and the form fails. The
> formBean was originally populated by /initAction.do
> In order to preserve the myAction formBean I have to make its scope
> "session".
</snip>

Based on the rest of your email, this is what should be happening:

  1) initAction.do is populating myActionForm with data from your Model 
layer
  2) Form1.jsp pulls the values out of myActionForm and displays them to 
the user
  3) The request ends, and the myActionForm instance created in (1) is 
destroyed
  4) The user makes changes to the form values on Form1.jsp and submits 
the changes to the server
  5) Struts creates a new instance of myActionForm and populates it with 
values from the request. *If Form1.jsp was written correctly, all of the 
values populated from the Model in (1) along with any user changes from 
(4) should be in the request*
  6) myActionForm is validated.  If the validation fails, control is 
returned to (3) with the data from (4)

I'm a little unclear as to where exactly your problem is, but it seems 
that some or all of the values from the original myActionForm are not 
getting written to the user, and therefore are not being submitted with 
the second request to populate the second instance of myActionForm.

-- Jeff


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