This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts 2, checking for field errors and then changing the target of a form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2, checking for field errors and then changing the target of a form" Watch "Struts 2, checking for field errors and then changing the target of a form" New topic
Author

Struts 2, checking for field errors and then changing the target of a form

michael lisser
Greenhorn

Joined: Dec 14, 2003
Posts: 5
Hi All,

I am using Struts 2, and I have a form, that is displayed in an iframe. When submitted the form should, once finished processing, update the parent. This I have achieved through setting target="_parent" in the form tag on that page.

However if there are validation errors, such as a field in the form is not correctly entered. Then I dont wish to update the parent as I would be expecting an error to be returned from the validator.

So firstly is there any way of controlling this kind of behaviour, such as if correctly processed, update parent, if incorrectly processed update self...



Secondly if there is no simple way of achieving this, can I change the form so that if an error is returned it doesnt continue to set the target for the form as the parent. Because I have several nested iframes consecutive errors progress up throuh the various nested iframes destroying/replacing them. I could live with this occuring once, but not subsequent times...

I can check if an error is returned easily enough, but so far have not been able to alter the form tag in a way that would work....

for example what should work is something like this...


However this fails to compile complaining of a missing </s:form> tag, even though one exists.

Similarly if I do the check within the form tag it also fails....



Complaining that the s:form tag is not correctly formatted.

While neither of the two above code fragments are a perfect solution (far from it) they would at least solve the immediate problem. But neither seem to pass the compiler...

Edited: Oops, I am guessing that the first code fragment fails because I am incorrectly nexting the tags, that the </s:form> tag needs to be within the if statement.

However if I do the same thing through a standard JSP code fragment such as:



It still fails...

Thanks

Michael



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Personally, I think you're going about this all wrong... If validation fails, you need to display the same form again. If it doesn't, you need to do something in a different frame. It's unlikely there's an easy way to do this using plain S2 tags.

Options include:
- using a plain form tag, so you can manipulate its attributes using custom tags
- validating via ajax, so the form will *always* submit successfully
- do everything via ajax and use JavaScript to do the "real work"

I'm sure there are more options beyond that as well.
michael lisser
Greenhorn

Joined: Dec 14, 2003
Posts: 5
Sorry, maybe I didnt make that quite clear.

When validation fails, I am using the same form. Hence I get back the form, with the validation errors.

However because when I submitted the form I set the "target=_parent" attribute on the form tag, this same form is now being displayed in the parent iframe. This is the correct iframe if the form is processed successfully, and returns a meaningful result. But not so good if it returns an error, as now I have the original form displayed in the parent iframe.

Using a plain form tag, does have some success, but seems to mess up all my other custom tags, as there is no longer any formatting, and as I have a lot of forms I would rather not be rewriting every page etc.

Going via Ajax is probably a better solution, but again not one I wish to progress just yet.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Copy the output created by the S2 form tag.
michael lisser
Greenhorn

Joined: Dec 14, 2003
Posts: 5
Thanks David,

Unfortunately that was also an approach I already tried. You would think it would work.... but unfortunately it doesnt.

When you change the S2 form tag to a standard form tag the subsequent S2 tags get reformatted/misformatted.

For example... with an S2 form tag...



becomes...



However without the S2 form tag, and using the equivalent standard tag the same code segment becomes...



This kind of thing is repeated throughout the page. Clearly something is in the S2 form tag that is used to transform the subsequent tags, I would suspect the "theme" as there is nothing else in it, but only guessing on that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2, checking for field errors and then changing the target of a form
 
Similar Threads
Accessing a beanList Returned from JSP
struts 2 validation
Struts 2 Iterate a List of Objects
problem in field validation
How to use CSS with Struts Tags?