4

I am learning myself Spring MVC 2.5 mostly from the docs. Can someone explain the following:

  • Advantages/differences of using command objects versus using @ModelAttribute to 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

2 Answers 2

3
  • The question about command object is not very clear. If you mean the following syntax

    @RequestMapping(...) public ModelAndView foo(Command c) { ... }
    

    then it is the same as the following

    @RequestMapping(...) public ModelAndView foo(@ModelAttribute Command c) { ... }
    

    because @ModelAttribute can be omitted. The only case when it's actually needed is then you need to specify attribute name explicitly (otherwise it would be inferred as a class name with the first letter decapitalized, i.e. command)

  • In Spring 2.5 - no. In Spring 3.0 you can use declarative validation with JSR-303 Bean Validation API.

  • The Errors object has a reference to the object being validated.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess my understanding of command object isnt clear. I thought that with @ModelAttribute you could annotate an object that will contain properties found in the form, and those properties will be set for you. for example @ModelAttribute User user would give you a prepopulated user object from the registration form. This is what i want to do. But whats the point of the Command object?
@mkoryak: I used the name Command as a placeholder for your own object, such as User in your example.
1

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.

1 Comment

nice. so commandName is something old that i dont care about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.