permaculture playing cards*
The moose likes Struts and the fly likes How to avoid validation if request was done by GET method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to avoid validation if request was done by GET method?" Watch "How to avoid validation if request was done by GET method?" New topic
Author

How to avoid validation if request was done by GET method?

Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
I have a form. And I want to have one url for form itself and for submitting it.
Controller should look at method and if it is GET, show form, if POST - make validations and so on.

How can I do it ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What version of Struts?
Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
Struts 2, I also use convention plugin.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's nothing to do that built-in; what I've usually done (because it's really easy :) is use the @Action annotation to map either the input method or a "process" method that'll do validation. I suppose you could do something similar with an interceptor, although I've never tried it with Convention, only a "plain" S2 app.
Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
>> what I've usually done is use the @Action annotation to map either the input method or a "process" method that'll do validation.

I don't understand anything
Could you explain more precisely, please.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Rather than specifically checking the request type ("GET" or "POST") I've just had different action mappings to display the form and process the form submission.

In Struts 1 I had a base action class that checked for GET/POST and would call validation manually on a POST. Struts 2's architecture makes this impractical on the action side.

In Struts 2 there are (at least) two simple approaches: interceptor side, and action mapping side.

On the interceptor side it's trivial to tweak the validation interceptor to skip validation on GET requests--essentially creating an exact duplicate of the shipping validation interceptor plus a request type check.

On the action mapping side you could display the form with one action mapping and process it with another. The initial form display could be handled by the input() method (which validation skips) and processed with a process() (or re-mapped execute() method?) method, which validation wouldn't skip.

I don't think I've explained myself well--feel free to follow up.
Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
Please, look at my 1st post.

>> I want to have one url for form itself and for submitting it.

How does your approach satisfies this requirement ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It doesn't; I guess you'll have to use the other suggestion I made of creating or modifying an interceptor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid validation if request was done by GET method?