You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sonam Belbase <So...@morganstanley.com> on 2003/12/15 22:56:03 UTC

struts validation and dispatch action

Hi,

I've noticed that since validation is defined in the action mapping, it
will apply to all methods of
a dispatch action. But what if only one method in the dispatch action
requires field X to
be populated/selected and another method doesn't care. Is there a way to
apply struts
validation to only one dispatch action method and not all of them?

Thanks,
SB

--
NOTICE: If received in error, please destroy and notify sender.  Sender
does not waive confidentiality or privilege, and use is prohibited.



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


Re: struts validation and dispatch action

Posted by Joe Germuska <Jo...@Germuska.com>.
>Joe,
>I'd like to try your duplicate action mapping suggestion, but I'm not
>sure how to specify the different paths. If my dispatchAction
>NotesAction.java
>has two methods - SaveNotes and CreateNotes, with CreateNotes needing the
>validation.
>what would the action mappings look like?

I'm sorry, I may not have read your initial post closely enough; some 
kinds of DispatchActions (like MappingDispatchAction) use a different 
mapping (and thus a different URL path) to activate each different 
piece of functionality in an action.  This is nice to minimize the 
number of classes when you have a bundle of closely related behaviors 
and such.

On the other hand, there are DispatchActions which dispatch based on 
request parameters and such -- for one of these, my "easiest" 
solution wouldn't apply.  In this case, you'd be best off just 
setting "validate" to false in struts-config and moving the call to 
form.validate into your action class...

Joe



>Thanks,
>SB
>
>Joe Germuska wrote:
>
>>  At 4:56 PM -0500 12/15/03, Sonam Belbase wrote:
>>  >Hi,
>>  >
>>  >I've noticed that since validation is defined in the action mapping, it
>>  >will apply to all methods of
>>  >a dispatch action. But what if only one method in the dispatch action
>>  >requires field X to
>>  >be populated/selected and another method doesn't care. Is there a way to
>>  >apply struts
>>  >validation to only one dispatch action method and not all of them?
>>
>>  The easiest thing would be to duplicate the ActionMapping in all
>>  respects except for the path and the validate attribute; then have
>>  your methods that don't require validation use the new path.
>>
>>  Alternatively, you could turn off validation and do what Struts does
>>  inside your own action:
>>
>>  ActionErrors errors = form.validate(mapping, request);
>>  if (errors.size != 0)
>>  {
>>     saveErrors(request,errors);
>>     return mapping.getInputForward();
>>  }
>>
>>  That's not 100% the same, but achieves the same goals in most simple
>>  cases.  See the "processValidate()" method of
>>  org.apache.struts.action.RequestProcessor to see every last thing
>>  that happens.  You often need to do this anyway when you can't easily
>>  encapsulate all of your validation in the form bean itself.
>>
>>  Hope this helps
>>          Joe
>>
>>  --
>>  Joe Germuska
>>  Joe@Germuska.com
>>  http://blog.germuska.com
>>    "We want beef in dessert if we can get it there."
>>     -- Betty Hogan, Director of New Product Development, National
>>  Cattlemen's Beef Association
>>
>>  ---------------------------------------------------------------------
>>  To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>  For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>--
>NOTICE: If received in error, please destroy and notify sender.  Sender does
>not waive confidentiality or privilege, and use is prohibited.
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org


-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
  "We want beef in dessert if we can get it there."
   -- Betty Hogan, Director of New Product Development, National 
Cattlemen's Beef Association


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


Re: struts validation and dispatch action

Posted by Sonam Belbase <So...@morganstanley.com>.
Thanks for all the suggestions.

Joe,
I'd like to try your duplicate action mapping suggestion, but I'm not
sure how to specify the different paths. If my dispatchAction
NotesAction.java
has two methods - SaveNotes and CreateNotes, with CreateNotes needing the
validation.
what would the action mappings look like?

Thanks,
SB

Joe Germuska wrote:

> At 4:56 PM -0500 12/15/03, Sonam Belbase wrote:
> >Hi,
> >
> >I've noticed that since validation is defined in the action mapping, it
> >will apply to all methods of
> >a dispatch action. But what if only one method in the dispatch action
> >requires field X to
> >be populated/selected and another method doesn't care. Is there a way to
> >apply struts
> >validation to only one dispatch action method and not all of them?
>
> The easiest thing would be to duplicate the ActionMapping in all
> respects except for the path and the validate attribute; then have
> your methods that don't require validation use the new path.
>
> Alternatively, you could turn off validation and do what Struts does
> inside your own action:
>
> ActionErrors errors = form.validate(mapping, request);
> if (errors.size != 0)
> {
>    saveErrors(request,errors);
>    return mapping.getInputForward();
> }
>
> That's not 100% the same, but achieves the same goals in most simple
> cases.  See the "processValidate()" method of
> org.apache.struts.action.RequestProcessor to see every last thing
> that happens.  You often need to do this anyway when you can't easily
> encapsulate all of your validation in the form bean itself.
>
> Hope this helps
>         Joe
>
> --
> Joe Germuska
> Joe@Germuska.com
> http://blog.germuska.com
>   "We want beef in dessert if we can get it there."
>    -- Betty Hogan, Director of New Product Development, National
> Cattlemen's Beef Association
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org

--
NOTICE: If received in error, please destroy and notify sender.  Sender does
not waive confidentiality or privilege, and use is prohibited.



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


Re: struts validation and dispatch action

Posted by Joe Germuska <Jo...@Germuska.com>.
At 4:56 PM -0500 12/15/03, Sonam Belbase wrote:
>Hi,
>
>I've noticed that since validation is defined in the action mapping, it
>will apply to all methods of
>a dispatch action. But what if only one method in the dispatch action
>requires field X to
>be populated/selected and another method doesn't care. Is there a way to
>apply struts
>validation to only one dispatch action method and not all of them?

The easiest thing would be to duplicate the ActionMapping in all 
respects except for the path and the validate attribute; then have 
your methods that don't require validation use the new path.

Alternatively, you could turn off validation and do what Struts does 
inside your own action:

ActionErrors errors = form.validate(mapping, request);
if (errors.size != 0)
{
   saveErrors(request,errors);
   return mapping.getInputForward();
}


That's not 100% the same, but achieves the same goals in most simple 
cases.  See the "processValidate()" method of 
org.apache.struts.action.RequestProcessor to see every last thing 
that happens.  You often need to do this anyway when you can't easily 
encapsulate all of your validation in the form bean itself.

Hope this helps
	Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
  "We want beef in dessert if we can get it there."
   -- Betty Hogan, Director of New Product Development, National 
Cattlemen's Beef Association


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