You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Brian Hawkins <br...@gmail.com> on 2008/04/29 21:26:18 UTC

Handling form data

I've run into a situation that I would like to get others opinions on.

The problem is with handling a form.  Here is the scenario: I have a form
called form.jsp, this form needs data from the database so it has an
associated FormAction.java class.  The struts.xml file looks like this:

<action name="myForm" class="FormAction">
<result>/form.jsp</result>
</action>

The form then posts the data to another action called postForm and the
struts.xml looks like this:

<action name="postForm" class="PostFormAction">
<result>...</result>
</action>

Now for the issues.  The first issue is field validation.  I can't just add
an input result that points to /form.jsp because the form needs the data out
of FormAction.  If I redirect to myForm I loose the field validation and the
same goes for chaining (I know I can hack out the errors from the stack but
that is ugly).  Using the session to save error info on is also ugly.

My preference is to use just one action class and have two methods.  I would
use execute() to get the data to view the form and postData() for posting
the data.  This way if postData found a field errors it could call execute
and then return an input status.  This solution still has problems in terms
of interceptor validators.  The validator needs to check for required fields
only when the postData() method is called and not when execute is called and
if there is an error when calling postData() it needs to call execute before
returning an input status.  I would like to make a validator that is smart
enough to know when to validate fields and when not to based on the method
being called but I cannot find a way to get what method on the action is
being called.

Is there a best practices on how to handle form data in an elegant manner?
Is there a way to know what method is being called?
So many questions and so few answers :(

thanks
Brian

Re: Handling form data

Posted by Greg Lindholm <gl...@yahoo.com>.
This should help you 
http://struts.apache.org/2.0.11.1/docs/how-do-we-repopulate-controls-when-validation-fails.html
FAQ:How do we repopulate controls when validation fails .
You can use the Preparable interface to load the data from the database.
Validation does not (by default config) run on result of "input".



Brian Hawkins-3 wrote:
> 
> I've run into a situation that I would like to get others opinions on.
> 
> The problem is with handling a form.  Here is the scenario: I have a form
> called form.jsp, this form needs data from the database so it has an
> associated FormAction.java class.  The struts.xml file looks like this:
> 
> <action name="myForm" class="FormAction">
> <result>/form.jsp</result>
> </action>
> 
> The form then posts the data to another action called postForm and the
> struts.xml looks like this:
> 
> <action name="postForm" class="PostFormAction">
> <result>...</result>
> </action>
> 
> Now for the issues.  The first issue is field validation.  I can't just
> add
> an input result that points to /form.jsp because the form needs the data
> out
> of FormAction.  If I redirect to myForm I loose the field validation and
> the
> same goes for chaining (I know I can hack out the errors from the stack
> but
> that is ugly).  Using the session to save error info on is also ugly.
> 
> My preference is to use just one action class and have two methods.  I
> would
> use execute() to get the data to view the form and postData() for posting
> the data.  This way if postData found a field errors it could call execute
> and then return an input status.  This solution still has problems in
> terms
> of interceptor validators.  The validator needs to check for required
> fields
> only when the postData() method is called and not when execute is called
> and
> if there is an error when calling postData() it needs to call execute
> before
> returning an input status.  I would like to make a validator that is smart
> enough to know when to validate fields and when not to based on the method
> being called but I cannot find a way to get what method on the action is
> being called.
> 
> Is there a best practices on how to handle form data in an elegant manner?
> Is there a way to know what method is being called?
> So many questions and so few answers :(
> 
> thanks
> Brian
> 
> 

-- 
View this message in context: http://www.nabble.com/Handling-form-data-tp16968494p16982984.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Handling form data

Posted by Laurie Harper <la...@holoweb.net>.
First, I should point out that an 'input' result of type 'chain' should 
work here. What makes you think it doesn't? You wont get a browser 
redirect with chaining, though, so if you want that the 
MessageStoreInterceptor can help with preserving error messages.

You can configure which methods skip validation with the excludeMethods 
parameter for the workflow and validation interceptors. Which methods 
are skipped by default varies with the different stacks defined in 
struts-defaults.xml, but assuming you're using defaultStack, it's 
input,back,cancel,browse.

I'd suggest looking at the example applications to see how these issues 
are handled there for more ideas.

L.

Brian Hawkins wrote:
> I've run into a situation that I would like to get others opinions on.
> 
> The problem is with handling a form.  Here is the scenario: I have a form
> called form.jsp, this form needs data from the database so it has an
> associated FormAction.java class.  The struts.xml file looks like this:
> 
> <action name="myForm" class="FormAction">
> <result>/form.jsp</result>
> </action>
> 
> The form then posts the data to another action called postForm and the
> struts.xml looks like this:
> 
> <action name="postForm" class="PostFormAction">
> <result>...</result>
> </action>
> 
> Now for the issues.  The first issue is field validation.  I can't just add
> an input result that points to /form.jsp because the form needs the data out
> of FormAction.  If I redirect to myForm I loose the field validation and the
> same goes for chaining (I know I can hack out the errors from the stack but
> that is ugly).  Using the session to save error info on is also ugly.
> 
> My preference is to use just one action class and have two methods.  I would
> use execute() to get the data to view the form and postData() for posting
> the data.  This way if postData found a field errors it could call execute
> and then return an input status.  This solution still has problems in terms
> of interceptor validators.  The validator needs to check for required fields
> only when the postData() method is called and not when execute is called and
> if there is an error when calling postData() it needs to call execute before
> returning an input status.  I would like to make a validator that is smart
> enough to know when to validate fields and when not to based on the method
> being called but I cannot find a way to get what method on the action is
> being called.
> 
> Is there a best practices on how to handle form data in an elegant manner?
> Is there a way to know what method is being called?
> So many questions and so few answers :(
> 
> thanks
> Brian
> 


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