title: Spring MVC: please explain difference between @RequestParam and @ModelAttribute
body: <p>I'm new to Spring MVC. Please help me unpack the documentation.</p>
  
  <p><strong>Documentation</strong></p>
  
  <p><a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html" rel="noreferrer">Spring MVC Documentation</a> states (emphasis mine):</p>
  
  <ul>
  <li><p><code>@ModelAttribute</code> on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model. Once present in the model, <strong>the argument’s fields should be populated from all request parameters that have matching names</strong>. The WebDataBinder class matches request parameter names — including query string parameters and form fields — to model attribute fields by name. </p></li>
  <li><p><code>@RequestParam</code> <strong>binds request parameters to a method parameter</strong> in your controller.</p></li>
  </ul>
  
  <p><strong>Disclaimer / Clarifier</strong></p>
  
  <p>I know that <code>@ModelAttribute</code> and <code>@RequestParam</code> are not the same thing, are not mutually exclusive, do not perform the same role, and can be used simultaneously, as in <a href="https://stackoverflow.com/questions/11268470/how-to-pass-requestparam-and-modelattribute-in-one-form">this question</a> - indeed, <code>@RequestParam</code> can be used to populate fields of <code>@ModelAttribute</code>. My question is more geared towards the difference between their internal workings.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>What is the difference between <code>@ModelAttribute</code> (used on a method argument, not method) and <code>@RequestParam</code>? Specifically:</p>
  
  <ul>
  <li><strong>Source:</strong> Do <code>@RequestParam</code> and <code>@ModelAttribute</code> have the same source of
  information / population, i.e. request parameters in URL, which may have been supplied as elements of a form / model that was <code>POST</code>ed? </li>
  <li><strong>Usage:</strong> Is it correct that variables retrieved with <code>@RequestParam</code> are thrown away (unless passed into a model), whereas variables retrieved with <code>@ModelAttribute</code> are automatically fed into the model to be returned?</li>
  </ul>
  
  <p>Or in very basic coding examples, what is the real working difference between these two examples?</p>
  
  <p>Example 1: <code>@RequestParam</code>:</p>
  
  <pre><code>// foo and bar are thrown away, and are just used (e.g.) to control flow?
  @RequestMapping(method = RequestMethod.POST)
  public String testFooBar(@RequestParam("foo") String foo,
  @RequestParam("bar") String bar, ModelMap model) {
      try {
       doStuff(foo, bar);
      }
      // other code
    }
  </code></pre>
  
  <p>Example 2: <code>@ModelAttribute</code>:</p>
  
  <pre><code>// FOOBAR CLASS
  // Fields could of course be explicitly populated from parameters by @RequestParam
  public class FooBar{
      private String foo;
      private String bar;
     // plus set() and get() methods
  }
  
  // CONTROLLER
  // Foo and Bar become part of the model to be returned for the next view?
  @RequestMapping(method = RequestMethod.POST)
  public String setupForm(@ModelAttribute("fooBar") FooBar foobar) {
     String foo = fooBar.getFoo();
     String bar = fooBar.getBar();
     try {
        doStuff(foo, bar);
     }
     // other code
  }
  </code></pre>
  
  <hr>
  
  <p><strong>My current understanding:</strong></p>
  
  <p><code>@ModelAttribute</code> and <code>@RequestParam</code> both interrogate the request parameters for information, but they use this information differently:</p>
  
  <ul>
  <li><p><code>@RequestParam</code> just populates stand-alone variables (which may of course be fields in a <code>@ModelAttribute</code> class). These variables will be thrown away when the Controller is done, unless they have been fed into the model.</p></li>
  <li><p><code>@ModelAttribute</code> populates the fields of a class, which then populates an attribute of the model to be passed back to the view</p></li>
  </ul>
  
  <p>Is this correct?</p>