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 Validation with LookupDispatchAction 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 "Validation with LookupDispatchAction" Watch "Validation with LookupDispatchAction" New topic
Author

Validation with LookupDispatchAction

Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Hello All,

I'm currently creating a class which extends LookupDispatchAction and uses only one form bean for several different forms, and each form has different validation requirements. Is it possible to use the struts validation framework by making the bean extend ValidatorForm and without implementing a validate method within the form bean?

Thanks!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
While the way you've designed your application may meet your needs in other ways, it is not very well suited for the use of the Struts Validation framework. With the validation framework, one has the ability to specify which validations will be performed based on either the name of the form or the action path. In your case where you're using both a generic form and a generic action, there's really no way for the validation framework to tell what should be validated when.

Unless you change your design, I would see using the ActionForm's validate method as the best method of validation. In that method you can include complex logic to determine which page is actually being presented, and which validations need to be performed.


Merrill
Consultant, Sima Solutions
Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Thanks for the response!

If I use the validate method in my ActionForm, do I need to use validator.xml and validator-rules.xml?

Thanks!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
No. Just write regular java routines to check validations. Struts will call the setters on the ActionForm bean prior to calling the validate method, so the properties of the bean will be populated with the user input. You can therefore use the bean properties rather than using request.getParameter().
Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Thanks again for your responses...I'm still a bit confused. I should have both a validate method in the ActionForm class as well as validation methods in my Action class?

Will I set my bean from the form and validate as follows?

Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Also, should I set validate=true in my action mapping?

Thanks again!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes. This controls whether or not Struts will call the validate method of your ActionForm. If an Action is called by a URI instead of a form submission, set it to false. That way you won't get "false alarm" validation messages on fields that could not possibly have been filled in yet. Also note that you need to supply an input attribute in your action mappings. This is how Struts knows which JSP to return to if validation fails.
Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Thank you!
Vladimir Grigoriev
Greenhorn

Joined: Feb 01, 2007
Posts: 1
Can you explain how i can dinamic change validate="false" from false (URI string) to true (submit button)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validation with LookupDispatchAction
 
Similar Threads
few basic question..
DynaValidatorForm and Dispatch?
validator in LookupDispachAction
How to Reverse Lookup resource key from resource Message in Struts
No validation before LookupDispatchAction