You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by boraldo <bo...@hotbox.ru> on 2009/03/25 20:54:38 UTC

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

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 ?
-- 
View this message in context: http://www.nabble.com/How-to-avoid-validation-if-request-was-done-by-GET-method--tp22709587p22709587.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: How to avoid validation if request was done by GET method?

Posted by "Shannon, Andrew" <an...@pqa.com>.
We also have a pattern for many situations where we encapsulate our
page-centric handling into a single action where for example a GET into
the action sets up the page, and a POST to the same action for form
handling but to a different method in the action.  Our approach to avoid
validation on the Get is that we have taken to writing most of our
validation as java code in the validate() method of an action that
implements validation aware, and have gotten away from plugging in
validators to objects for this particular reason.  So what we do is:

public void validate() {
	String actionMethod =
ActionContext.getContext().getActionInvocation().getProxy().getMethod();

	if
(actionMethod.equals("myActionMethodConfiguredAsTheFormPostTarget")) {
		// do some validation
	}
}

This also allows us to delegate validation to other classes needed to
handle more complex validation scenarios.

-----Original Message-----
From: benjamin haimerl [mailto:b-skillz@gmx.de] 
Sent: Thursday, March 26, 2009 1:34 PM
To: Struts Users Mailing List
Subject: Re: How to avoid validation if request was done by GET method?

hi boraldo

a very simple way should be:


Action:

YourActionConnetedToTheFormAction extends Action..{

execute(..){

String qString = request.getQueryString();
if(qString!=null && paramsFound(...))
   // *paramsFound() = check if your formParams were given by query
String
}
else {
//check if your formBean is set (depends on the scope you have set in 
your struts-config.xml)
//e.g.
  if(request.getAttribute(yourForm)!=null) {
  yourForm.validate() {
}
 }
}

}

should work ;)



boraldo wrote:
> 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 ?
>   



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


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

Posted by benjamin haimerl <b-...@gmx.de>.
its the default validation method...

- if you use the Validation Framework ( FormBean extends ValidationForm)
its validation method will be called(validate all  fields  against the 
configuration in validation.xml) 
and all errors will be added to the default  ActionMessage Object

- if u use your own validate method its in your hand what happens if 
some errors occur
maybe adding the error with
ActionMessages msgs = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new 
ActionMessage("error.xxx.xxx"));
or something like that



boraldo wrote:
> Do I understand properly that ActionSupport.validate method that you used
> here performs the same validation as validation interceptor ?
>
> How can I see if there were validation errors ?
>
>
>
> benjamin haimerl wrote:
>   
>> hi boraldo
>>
>> a very simple way should be:
>>
>>
>> Action:
>>
>> YourActionConnetedToTheFormAction extends Action..{
>>
>> execute(..){
>>
>> String qString = request.getQueryString();
>> if(qString!=null && paramsFound(...))
>>    // *paramsFound() = check if your formParams were given by query String
>> }
>> else {
>> //check if your formBean is set (depends on the scope you have set in 
>> your struts-config.xml)
>> //e.g.
>>   if(request.getAttribute(yourForm)!=null) {
>>   yourForm.validate() {
>> }
>>  }
>> }
>>
>> }
>>
>> should work ;)
>>
>>
>>
>> boraldo wrote:
>>     
>>> 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 ?
>>>   
>>>       
>> ---------------------------------------------------------------------
>> 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: How to avoid validation if request was done by GET method?

Posted by boraldo <bo...@hotbox.ru>.
Do I understand properly that ActionSupport.validate method that you used
here performs the same validation as validation interceptor ?

How can I see if there were validation errors ?



benjamin haimerl wrote:
> 
> hi boraldo
> 
> a very simple way should be:
> 
> 
> Action:
> 
> YourActionConnetedToTheFormAction extends Action..{
> 
> execute(..){
> 
> String qString = request.getQueryString();
> if(qString!=null && paramsFound(...))
>    // *paramsFound() = check if your formParams were given by query String
> }
> else {
> //check if your formBean is set (depends on the scope you have set in 
> your struts-config.xml)
> //e.g.
>   if(request.getAttribute(yourForm)!=null) {
>   yourForm.validate() {
> }
>  }
> }
> 
> }
> 
> should work ;)
> 
> 
> 
> boraldo wrote:
>> 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 ?
>>   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-avoid-validation-if-request-was-done-by-GET-method--tp22709587p22727895.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: How to avoid validation if request was done by GET method?

Posted by benjamin haimerl <b-...@gmx.de>.
hi boraldo

a very simple way should be:


Action:

YourActionConnetedToTheFormAction extends Action..{

execute(..){

String qString = request.getQueryString();
if(qString!=null && paramsFound(...))
   // *paramsFound() = check if your formParams were given by query String
}
else {
//check if your formBean is set (depends on the scope you have set in 
your struts-config.xml)
//e.g.
  if(request.getAttribute(yourForm)!=null) {
  yourForm.validate() {
}
 }
}

}

should work ;)



boraldo wrote:
> 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 ?
>   


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