I use validate() method from ActionSupport for complex bussines
validation.(it works in WorkFlowInterceptor). 

Sometime, for  trivial fields, i can use Struts ValidationFramework.(it
works after ParameterInterceptor)



Sean Kleinjung-2 wrote:
> 
> Hey,
> 
> I have a question concerning where validation should be performed, and
> what people feel best practices are. Specifically, I have a number of
> business objects that validate parameters supplied to their setters
> for correctness, and throw IllegalArgumentExceptions if something is
> invalid. For example, in a hypothetical Person class I would have:
> 
>     ...
>     public void setAge(int age) {
>         if (age < 0) throw new MyInvalidValueException("some i18n-ed
> error msg");
>         this.age = age;
>     }
>     ....
> 
> This is done so that if there is a failure to properly validate at
> some other layer of the application, it will fail-fast and allow the
> problem to be debugged more easily. (It also gives confidence that all
> references to a business object do not contain invalid data.)
> 
> Now, we are using Struts2 and intend to use the provided annotations
> for validation. The problem is, if I create the app so that HTTP
> parameters are set directly on the business objects (by using a 'name'
> value of "person.name" for a form field, for example), these
> exceptions will prevent the value from being set, so validation is
> never performed (and no errors are set on the action).
> 
> The way I see it, I have three options:
> 
> 1) Remove all validity checks from the business objects, and rely on
> Struts to validate incoming data, and the DAO layer to validate them
> before inserting. This would probably be the simplest option, but I
> wanted to get peoples thoughts on it first. Are argument checks like
> the ones above a good idea? Or is it better to have these type of
> objects be dumb-data transfer objects that will carry any value they
> are given?
> 
> 2) Duplicate all properties of the business objects on the struts
> actions, where they can be validated using the validation annotations.
> The action methods must then copy the values into the business objects
> as needed. This would allow the business objects to remain unchanged,
> but has a lot of duplication and maintenance overhead if fields are
> added/removed/changed. (It basically reminds me of the nightmare of
> Struts1 form beans :p )
> 
> 3) Extend the ParametersInterceptor and/or validation interceptor to
> do some kind of "magic" involving catching the argument exceptions,
> setting appropriate errors on the action, and preserving the invalid
> values so that they can be redisplayed on the form. This option is
> definitely the most work (and I haven't looked at the Struts source
> closely enough to know exactly how much is involved, and if its even
> possibly to do in an elegant fashion).
> 
> So in addition to commenting on the above specific situation, the
> general question is this: should business objects be self-validating,
> and how should they handle validation errors?
> 
> Thanks in advance for any input,
> Sean Kleinjung
> AV Support, Inc.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Validation-Best-Practices--tf4685400.html#a13423610
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to