title: Understanding the use of @ModelAttribute and @RequestAttribute annotations in Spring MVC
body: <p>I am pretty new in Spring MVC. Currently I am studying the <a href="https://github.com/spring-projects/spring-mvc-showcase" rel="nofollow">Spring MVC Showcase</a>, that demonstrates the features of the Spring MVC web framework.</p>
  
  <p>I have some problem to understand how Custom Resolvable Web Arguments are handled in this example.</p>
  
  <p>In practice I have the following situation. In my <em>home.jsp</em> view I have the following link:</p>
  
  <pre><code>&lt;a id="customArg" class="textLink" href="&lt;c:url value="/data/custom" /&gt;"&gt;Custom&lt;/a&gt; 
  </code></pre>
  
  <p>This link generate an HTTP Request towards the URL: <strong>"/data/custom"</strong></p>
  
  <p>The controller class that contain the method that handles this request have the following code:</p>
  
  <pre><code>@Controller
  public class CustomArgumentController {
  
      @ModelAttribute
      void beforeInvokingHandlerMethod(HttpServletRequest request) {
          request.setAttribute("foo", "bar");
      }
  
      @RequestMapping(value="/data/custom", method=RequestMethod.GET)
      public @ResponseBody String custom(@RequestAttribute("foo") String foo) {
          return "Got 'foo' request attribute value '" + foo + "'";
      }
  
  }
  </code></pre>
  
  <p>The method that handles this HTTP Request is <strong>custom()</strong>. So when the previous link is clicked, the HTTP Request is handled by the custom method.</p>
  
  <p>I have some problem to understand what exactly does the <strong>@RequestAttribute</strong> annotation do. I think that, in this case, it binds the request attribute named foo to a new String foo variable. But where this attribute is taken from? Is this variable taken by Spring?</p>
  
  <p>OK, my idea is that this request attribute is taken from a HttpServletRequest object. I think so because, in this class, I also have the <strong>beforeInvokingHandlerMethod()</strong> method that have a speaking name, so it seems that this method sets an attribute, that have <em>name=foo</em> and <em>value=bar</em>, inside an HttpServletRequest object, and then so the custom() method can use this value.</p>
  
  <p>In fact my output is:</p>
  
  <blockquote>
    <p>Got 'foo' request attribute value 'bar'</p>
  </blockquote>
  
  <p>Why the <strong>beforeInvokingHandlerMethod()</strong> is called before the <strong>custom()</strong> method?</p>
  
  <p>And why the <strong>beforeInvokingHandlerMethod()</strong> is annotated by <strong>@ModelAttribute</strong> annotation? What does it mean in this case?</p>