File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Where is the best place to validate? XML, .validate() or JS? other? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Where is the best place to validate? XML, .validate() or JS? other?" Watch "Where is the best place to validate? XML, .validate() or JS? other?" New topic

Where is the best place to validate? XML, .validate() or JS? other?

Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
I've been struggling with this as a newbie...

should I validate input using the validate method? in XML using validator? via Javascript?

i'm trying to keep life simple for me, and I wonder what you guys think? has anyone done all 3 and can offer some wisdom as to what works best under which conditions?

Ideally, I'd like to go to one spot to validate and troubleshoot. As it is, I'm a little put off by the fact that Struts is not very easy to debug.


Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

In struts 1 I kept some of my validation in the validate method but in struts 2 I kept a lot of the simple validation with annotations.

I dont know that you want to particularly validate all your fields with JS as you can or may expose some of your internal rules to the client.
Joe Ess

Joined: Oct 29, 2001
Posts: 9191

I usually use XML validation unless the validation logic is fairly complex, then I override the validation method.
Many people prefer JavaScript validation as it saves a round-trip to the server. I'm not working on apps with high traffic or latency, so a round-trip is fine for my purposes. Lucas has a good point about exposing your validation logic to the client.
The "best" validation strategy is the one that fills your requirements. It may seem confusing when you are just starting out, but one day you'll be glad you have a flexible framework.

[How To Ask Questions On JavaRanch]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63872

Validation should always occur on the server regardless of whether any client-side validation takes place. Client-side validation is easy to circumvent.

Client-side validation helps with the user experience (quick feedback, no server round-trip) but should never be relied upon. The server code should always pretend that client-side validation does not exist.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Where is the best place to validate? XML, .validate() or JS? other?
It's not a secret anymore!