title: Spring MVC @ModelAttribute method
body: <p>Question about <code>Spring MVC</code> <code>@ModelAttribute</code> methods, Setting model attributes in a controller <code>@RequestMapping</code> method verses setting attribute individually with <code>@ModelAttribute</code> methods, which one is considered better and is more used?</p>
  
  <p>From design point of view which approach is considered better from the following: </p>
  
  <p>Approach 1</p>
  
  <pre><code>@ModelAttribute("message")
  public String addMessage(@PathVariable("userName") String userName, ModelMap model) {
  
    LOGGER.info("addMessage - " + userName);
    return "Spring 3 MVC Hello World - "  + userName;
  }
  
  @RequestMapping(value="/welcome/{userName}", method = RequestMethod.GET)
  public String printWelcome(@PathVariable("userName") String userName, ModelMap model) {
  
    LOGGER.info("printWelcome - " + userName);
    return "hello";
  }   
  </code></pre>
  
  <p>Approach 2 </p>
  
  <pre><code>@RequestMapping(value="/welcome/{userName}", method = RequestMethod.GET)
  public String printWelcome(@PathVariable("userName") String userName, ModelMap model) {
  
    LOGGER.info("printWelcome - " + userName);
  
    model.addAttribute("message", "Spring 3 MVC Hello World - "  + userName);
  
    return "hello";
  }   
  </code></pre>