You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Konrad Billewicz <kb...@infovide.pl> on 2005/12/27 21:00:20 UTC

Right way to handle compilcated form

Hello,

I have a complicated form with mulit-level menu (several <select>'s with 
onchange='submit()' ). I'm not sure how I should organize my Actions.

Currently I have PrepareEditXYZAction which handles the JSP page. Form in 
the JSP page routes back to PrepareEditXYZAction. Inside 
PrepareEditXYZAction action I'm making a decision (based on check if submit 
button was clicked) what to show -  JSP page (if submit wasn't clicked) or 
EditXYZAction (if it was). EditXYZAction is validating data and invokes 
managers to do the job.

I'm using Struts validation based on ValidationForm's. PrepareEditXYZAction 
has validate set to false while EditXYZAction has it set to true and input 
set to PrepareEditXYZAction (/prepareEditXYZAction.do).

It sounds nice but I have a problem when validation error occurs. In this 
case I have an infinite loop bacause:
1. PrepareEditXYZAction redirects to EditXYZAction (bacause submit was 
clicked)
2. Validation before EditXYZAction fails and redirects to 
PrepareEditXYZAction.
3. Go to 1.

I have several ideas how to handle this problem but I'm not sure if I'm 
doing it right. Do you know pattern for handling such cases? I look thought 
Struts manual, Struts in Action and the Web but found nothing interesting.

Best regards,
Konrad Billewicz




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


Re: Right way to handle compilcated form

Posted by Rick R <st...@reumann.net>.
Konrad Billewicz wrote:
> Hello,
> 
> I have a complicated form with mulit-level menu (several <select>'s with 
> onchange='submit()' ). I'm not sure how I should organize my Actions.

Konrad, I don't have a lesson behind this app yet, but you might want to 
get this zip I have and look at the struts-crud example that's inside (3 
other ones in there also that are basic). I think it provides a nice 
example of how I like to do the stuff you mention - setUp for an 
edit/insert etc. Basic CRUD stuff.

http://www.reumann.net/misc/lessons.zip


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


Re: Right way to handle compilcated form

Posted by Laurie Harper <la...@holoweb.net>.
Konrad Billewicz wrote:
>> Sounds like you have some broken flow control there... I would expect that 
>> PrepareEditXYZAction would forward to a JSP, and that the JSP would have a 
>> form that submits to EditXYZAction. EditXYZAction would have 'intput' set 
>> to point to PrepareEditXYZAction which would mean that on validation 
>> errors, PrepareEditXYZAction would be called and would forward to the JSP 
>> again. In other words, (1) sounds wrong.
> 
> I understand that the only thing you consider is not right is that decision 
> edit-or-update is made in PrepareEditXYZAction, not in EditXYZAction. I have 
> made this choice knowingly for several reasons:
 >
> 1. Prepare action should be responsible for preparing parameters, 
> compleately. I could image that there is another action which uses 
> complately different mechanism for getting parameters (for example getting 
> pure IDs from some source) but executes the same Edit action. I'm not sure 
> is it right idea but it sounds good in my opinion.

I'm not sure what you mean with this. Normally, you have an action which 
'prepares' the data the view will need to render, which then forwards to 
a JSP; and an action for the JSP to submit to, which performs the 
desired application logic for the form submission.

If you mean that you need to do some of the same setup work before 
displaying the view as well as before processing the form, you can 
either pass that data through the view so it's available from the 
request in your edit action, or have a helper class that both actions 
use to do that common setup work.

> 2. I had problems with validation. My form is multi-level and I want 
> validation only when parameters are complete. In mappings of Edit action I 
> have validation set to true, so any partial data form is rejected 
> automatically. I don't tend to resign from automatic validation bacause need 
> of such a resign seems to be, in my opinion, a design mistake.

So you have a multi-page wizard type scenario? I.e. one view which 
repeatedly submits to the same action to render the next page of the 
wizard? You can still have the final page in the flow submit to a 
separate 'the wizard is complete, process the submitted data' action.

> Currently I had solve my problem differently and I'm quite satisfied with 
> it. I have made third action, very ganeric, named RelayAction. More about it 
> can be found at [1]. What do you think about this solution?
> 
> [1] http://www.jguru.com/faq/view.jsp?EID=995474

Looks like just another way of managing control flow: a 'switching 
action' that works by chaining to another action, as opposed to by 
delegating to a method within the same action as DispatchAction does.

That needs to be used with caution; action chaining is generally 
discouraged. If I were to use that technique, I'd still carefully 
separate preparing and rendering the view from processing the data 
collected by the view.

It may help to remember that forward names are *logical outcomes* of 
action processing, not simply an alias for a URL. Ted's article doesn't 
make that distinction very clearly (well, at least not that particular 
article :-)

>> Can you post your action mapping definitions from struts-config, and the 
>> relevant bits of your JSP code (specifically the html:form tag)?
> 
> Mappings:

Well your JSP references an action you didn't show :-) But given what 
you say above, I think what you're doing is calling an action with 
forwards to a JSP; having that JSP submit to the same action; and having 
that action forward to another action to process the form. If validation 
fails on the second action to forwards to the first one (via it's 
'input' mapping), which promptly tries to forward to the second one 
again because it sees the same 'dispatch' parameter in the request.

This is one reason why action chaining is bad :-)

What you should do instead is have the JSP submit directly to the second 
action and remove the action chaining logic from the first action. Then 
if the edit action fails validation and forwards to the first action, 
it'll just redisplay the JSP as intended.

If you really need to execute (some of) the same logic in both actions 
see above: move it into a shared helper class.

HTH,

L.


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


Re: Right way to handle compilcated form

Posted by Konrad Billewicz <kb...@infovide.pl>.
> Sounds like you have some broken flow control there... I would expect that 
> PrepareEditXYZAction would forward to a JSP, and that the JSP would have a 
> form that submits to EditXYZAction. EditXYZAction would have 'intput' set 
> to point to PrepareEditXYZAction which would mean that on validation 
> errors, PrepareEditXYZAction would be called and would forward to the JSP 
> again. In other words, (1) sounds wrong.

I understand that the only thing you consider is not right is that decision 
edit-or-update is made in PrepareEditXYZAction, not in EditXYZAction. I have 
made this choice knowingly for several reasons:
1. Prepare action should be responsible for preparing parameters, 
compleately. I could image that there is another action which uses 
complately different mechanism for getting parameters (for example getting 
pure IDs from some source) but executes the same Edit action. I'm not sure 
is it right idea but it sounds good in my opinion.
2. I had problems with validation. My form is multi-level and I want 
validation only when parameters are complete. In mappings of Edit action I 
have validation set to true, so any partial data form is rejected 
automatically. I don't tend to resign from automatic validation bacause need 
of such a resign seems to be, in my opinion, a design mistake.

Currently I had solve my problem differently and I'm quite satisfied with 
it. I have made third action, very ganeric, named RelayAction. More about it 
can be found at [1]. What do you think about this solution?

[1] http://www.jguru.com/faq/view.jsp?EID=995474

> Can you post your action mapping definitions from struts-config, and the 
> relevant bits of your JSP code (specifically the html:form tag)?

Mappings:

<!-- this action prepares parameters for the edit -->
<action path="/prepareEditNewEmailForNewCase"
type="pl.infovide.portlet.admin.email.action.PrepareEditNewEmailForNewCaseAction"
name="newEmailForm"
validate="false"
scope="session"
>
<forward name="update" path="/email/editNewEmailForNewCase.jsp" />
<forward name="edit" path="/editNewEmailForNewCase.do" />
</action>

<!-- this action edits -->
<action path="/editNewEmailForNewCase"
type="pl.infovide.portlet.admin.email.action.EditNewEmailForNewCaseAction"
name="newEmailForm"
validate="true"
input="/prepareEditNewEmailForNewCase.do"
scope="session"
>
<forward name="success" path="/listNewEmails.do" />
<forward name="update" path="/prepareEditNewEmailForNewCase.do" />
</action>


and the JSP:

<html:form action="/submitEditNewEmailForNewCase.do">

...

<html:submit property="update">
<bean:message key="email.update" />
</html:submit>

<html:submit property="edit">
<bean:message key="email.submit" />
</html:submit>

</html:form>

Best regards,
Konrad Billewicz




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


Re: Right way to handle compilcated form

Posted by Laurie Harper <la...@holoweb.net>.
Konrad Billewicz wrote:
> Hello,
> 
> I have a complicated form with mulit-level menu (several <select>'s with 
> onchange='submit()' ). I'm not sure how I should organize my Actions.
> 
> Currently I have PrepareEditXYZAction which handles the JSP page. Form in 
> the JSP page routes back to PrepareEditXYZAction. Inside 
> PrepareEditXYZAction action I'm making a decision (based on check if submit 
> button was clicked) what to show -  JSP page (if submit wasn't clicked) or 
> EditXYZAction (if it was). EditXYZAction is validating data and invokes 
> managers to do the job.
> 
> I'm using Struts validation based on ValidationForm's. PrepareEditXYZAction 
> has validate set to false while EditXYZAction has it set to true and input 
> set to PrepareEditXYZAction (/prepareEditXYZAction.do).
> 
> It sounds nice but I have a problem when validation error occurs. In this 
> case I have an infinite loop bacause:
> 1. PrepareEditXYZAction redirects to EditXYZAction (bacause submit was 
> clicked)
> 2. Validation before EditXYZAction fails and redirects to 
> PrepareEditXYZAction.
> 3. Go to 1.
> 
> I have several ideas how to handle this problem but I'm not sure if I'm 
> doing it right. Do you know pattern for handling such cases? I look thought 
> Struts manual, Struts in Action and the Web but found nothing interesting.

Sounds like you have some broken flow control there... I would expect 
that PrepareEditXYZAction would forward to a JSP, and that the JSP would 
have a form that submits to EditXYZAction. EditXYZAction would have 
'intput' set to point to PrepareEditXYZAction which would mean that on 
validation errors, PrepareEditXYZAction would be called and would 
forward to the JSP again. In other words, (1) sounds wrong.

Can you post your action mapping definitions from struts-config, and the 
relevant bits of your JSP code (specifically the html:form tag)?

L.


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