title: spring mvc validation best practices
body: <p>I am learning myself Spring MVC 2.5 mostly from the docs. Can someone explain the following:</p>
  
  <ul>
  <li>Advantages/differences of using <code>command</code> objects versus using <code>@ModelAttribute</code> to pass in the object.</li>
  <li>Is there an easier way to do validation other then writing a Validator object?</li>
  </ul>
  
  <p>Also, in this code how does the line <code>ValidationUtils.rejectIfEmpty(e, "name", "name.empty");</code> work? How can it check if the name is empty on the <strong>person</strong> object when the person object is not passed in?</p>
  
  <pre><code>   public void validate(Object obj, Errors e) {
          ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
          Person p = (Person) obj;
          if (p.getAge() &lt; 0) {
              e.rejectValue("age", "negativevalue");
          } else if (p.getAge() &gt; 110) {
              e.rejectValue("age", "too.darn.old");
          }
      }
  </code></pre>
  
  <p>(this code is from section 5.2 from <a href="http://static.springsource.org/spring/docs/2.5.x/reference/validation.html" rel="nofollow">the docs</a>)</p>
  
  <p>Thanks</p>