I am learning myself Spring MVC 2.5 mostly from the docs. Can someone explain the following:
- Advantages/differences of using
commandobjects versus using@ModelAttributeto pass in the object. - Is there an easier way to do validation other then writing a Validator object?
Also, in this code how does the line ValidationUtils.rejectIfEmpty(e, "name", "name.empty"); work? How can it check if the name is empty on the person object when the person object is not passed in?
public void validate(Object obj, Errors e) {
ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
Person p = (Person) obj;
if (p.getAge() < 0) {
e.rejectValue("age", "negativevalue");
} else if (p.getAge() > 110) {
e.rejectValue("age", "too.darn.old");
}
}
(this code is from section 5.2 from the docs)
Thanks
Here is the answer of your first question.
http://chompingatbits.com/2009/08/25/spring-formtag-commandname-vs-modelattribute/
according to my experience there isn’t easier way to fullfill validation, because it’s easy enough. You can get it easier integrating libraries such as commons-validator into your project and use pre-defined validation rules in your forms.
http://numberformat.wordpress.com/tag/spring-mvc-validation/
and also with 3rd version of Spring you can use Bean validation using annotations.