title: Form submit in Spring MVC 3 - explanation
body: <p>I'm having problems understanding how does a form submit in Spring 3 MVC work.</p>
  
  <p>What I want to do, is to create a controller which would take the user's name and display it to him. And somehow I have done it but I don't really understand how it works. So..</p>
  
  <p>I have a form which looks like this:</p>
  
  <pre><code>&lt;form:form method="post" modelAttribute="person"&gt;
      &lt;form:label path="firstName"&gt;First name&lt;/form:label&gt;
      &lt;form:input path="firstName" /&gt;
      &lt;br /&gt;
  
      &lt;form:label path="lastName"&gt;Last name&lt;/form:label&gt;
      &lt;form:input path="lastName" /&gt;
      &lt;br /&gt;
  
      &lt;input type="submit" value="Submit" /&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>I also have a controller which looks like this:</p>
  
  <pre><code>@Controller
  public class HomeController {
  
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String showHelloPage(Model model) {
          model.addAttribute("person", new Person());
          return "home";
      }
  
      @RequestMapping(value = "/", method = RequestMethod.POST)
      public String sayHello(Person person, Model model) {
          model.addAttribute("person", person);
          return "home";
      }
  }
  </code></pre>
  
  <p>To display a welcome message to a user I use the following code in the JSP page:</p>
  
  <pre><code>&lt;c:if test="${not empty person.firstName and not empty person.lastName}"&gt;
      Hello ${person.firstName} ${person.lastName}!
  &lt;/c:if&gt;
  </code></pre>
  
  <p>And it works (I omit the XML configuration files because they are irrelevant to the problem).</p>
  
  <p>I thought that the "modelAttribute" attribute in a form points to the bean variable which should be populated with inputs' values (as set in their "path" attributes). But looks, it works in a very different way. If I remove the line</p>
  
  <pre><code>model.addAttribute("person", new Person());
  </code></pre>
  
  <p>from "showHelloPage" method I get an (common) exception "Neither BindingResult nor...".</p>
  
  <p>Also, on the beginning, the "sayHello" method looked like:</p>
  
  <pre><code>(...)
  public String sayHello(@ModelAttribute("person") Person person, Model model) {
  (...)
  </code></pre>
  
  <p>I mean, it had the "ModelAttribute" annotation. I added it, because in the tutorials I have read, it was always present. But after I removed it, everything worked well, as it did before.</p>
  
  <p><strong>So my question is</strong> - what is the use of the "ModelAttribute" anonnatation? Is it some way to omit a "modelAttribute" attribute in a form? And the second part, what is the way (maybe some annotation) to make a form automatically bind inputs' values to the proper bean's properties (which would be declared as a method parameter)? Without a need of adding an empty bean before sending a form (as I have to do it now).</p>
  
  <p>Thanks for your replies (which aren't links to the Spring's documentation, because I have already read it).</p>