title: Spring MVC validation doesn't work
body: <p>I want to implement easy validation with Spring MVC.</p>
  
  <p>I added to <code>pom.xml</code>:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;version&gt;4.2.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Here is my <strong><em>POJO</em></strong>:</p>
  
  <pre><code>import org.hibernate.validator.constraints.Range;
  
  public class Goal {
      @Range(min = 1, max = 120)
      private int minutes;
      // getters + setters
  </code></pre>
  
  <p>and <strong><em>Controller</em></strong> implementation:</p>
  
  <pre><code>import javax.validation.Valid;
  
  @Controller
  @SessionAttributes("goal")
  public class GoalController {
  
      @RequestMapping(value = "addGoal", method = RequestMethod.GET)
      public String addGoal(Model model) {
          Goal goal = new Goal();
          goal.setMinutes(10);
          model.addAttribute("goal", goal);
          return "addGoal";
      }
  
      @RequestMapping(value = "addGoal", method = RequestMethod.POST)
      public String updateGoal(@Valid @ModelAttribute(value = "goal") Goal goal, BindingResult result) {
          System.out.printf("Result has errors: %s%n", result.hasErrors());
          System.out.printf("Minutes updated: %d%n", goal.getMinutes());
          if (result.hasErrors()) {
              return "addGoal";
          }
          return "redirect:/addMinutes.html";
      }
  }
  </code></pre>
  
  <p><code>addGoal.jsp</code> page:</p>
  
  <pre><code>&lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  &lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Add Goal Here&lt;/title&gt;
      &lt;style&gt;
          .error {
              color: #ff0000;
          }
  
          .errorblock {
              color: #000;
              background-color: #ffeeee;
              border: 3px solid #ff0000;
              padding: 8px;
              margin: 16px;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  Language: &lt;a href="?language=en"&gt;English&lt;/a&gt; | &lt;a href="?language=uk"&gt;Ukrainian&lt;/a&gt;
  &lt;form:form commandName="goal"&gt;
      &lt;form:errors path="*" cssClass="errorblock" element="div" /&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;spring:message code="goal.minutes"/&gt;&lt;/td&gt;
              &lt;td&gt;&lt;form:input path="minutes"/&gt;&lt;/td&gt;
              &lt;td&gt;&lt;form:errors path="minutes" cssClass="error"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td colspan="3"&gt;
                  &lt;input type="submit" value="Enter Goal Minutes"/&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is page view:</p>
  
  <p><img src="https://i.stack.imgur.com/Jst2q.png" alt="enter image description here"></p>
  
  <p>And appropriate result:</p>
  
  <p><img src="https://i.stack.imgur.com/LA96y.png" alt="enter image description here"></p>
  
  <p>snippet from <strong><em>console:</em></strong></p>
  
  <pre><code>Result has errors: false
  Minutes updated: -5
  </code></pre>
  
  <p>It should display error message for negative input and stay at the same page instead second screen shot.</p>
  
  <p>I couldn't figure out causing of this trouble.</p>
  
  <p><strong><em>Any suggestion?</em></strong></p>