title: Spring MVC: Having multiple @ModelAttribute in form handling action
body: <h2>The context</h2>
  
  <p>I have a simple association between two entities - <code>Category</code> and <code>Email</code> (NtoM). I'm trying to create web interface for browsing and managing them. To browse the category and to add e-mails into that category I use controller wrapped with <code>@RequestMapping</code> with category ID (UUID), so all controller actions are always taking place in context of category specified with path.</p>
  
  <p>I use <code>@ModelAttribute</code> to pre-load context category for entire controller scope.</p>
  
  <h2>The problem</h2>
  
  <p>This approach worked well for listing and for displaying the forms. However it fails on form submission - after debugging a little, I found out that form data overrides my category <code>@ModelAttribute</code> parameter.</p>
  
  <p>In my code, in method <code>save()</code> the <code>category</code> is not really the model attribute loaded with <code>addCategory()</code> method, but is populated with form data (<code>email</code> model is also populated, and that is correct).</p>
  
  <p>I'm looking for the solution that will allow me to bind form data only to specific <code>@ModelAttribute</code>.</p>
  
  <p>I've read in Spring MVC documentation that order of arguments matters, but I ordered them accordingly to examples and still it doesn't work like expected.</p>
  
  <h2>The code</h2>
  
  <p>Here is my controller:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  @RequestMapping("/emails/{categoryId}")
  public class EmailsController
  {
      @ModelAttribute("category")
      public Category addCategory(@PathVariable UUID categoryId)
      {
          return this.categoryService.getCategory(categoryId);
      }
  
      @InitBinder
      public void initBinder(WebDataBinder binder)
      {
          binder.registerCustomEditor(Set.class, "categories", new CategoriesSetEditor(this.categoryService));
      }
  
      @RequestMapping(value = "/create", method = RequestMethod.GET)
      public String createForm(@ModelAttribute Category category, Model model)
      {
          // here everything works, as there is just a single @ModelAttribute
  
          return "emails/form";
      }
  
      @RequestMapping(value = "/save", method = RequestMethod.POST)
      public String save(
          @ModelAttribute @Valid Email email,
          BindingResult result,
          Model model,
          @ModelAttribute("category") Category category
      ) {
          // saving entity, etc
  
          // HERE! problem is, that response is bound BOTH to `email' and `category' model attributes
          // and overrides category loaded in `addCategory()' method
          return String.format("redirect:/emails/%s/", category.getId().toString());
      }
  }
  </code></pre>
  
  <p>Just in case here is also the form code:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;form:form action="${pageContext.request.contextPath}/emails/${category.id}/save" method="post" modelAttribute="email"&gt;
      &lt;form:hidden path="id"/&gt;
      &lt;fieldset&gt;
          &lt;label for="emailName"&gt;&lt;spring:message code="email.form.label.Name" text="E-mail address"/&gt;:&lt;/label&gt;
          &lt;form:input path="name" id="emailName" required="required"/&gt;
          &lt;form:errors path="name" cssClass="error"/&gt;
  
          &lt;label for="emailRealName"&gt;&lt;spring:message code="email.form.label.RealName" text="Recipient display name"/&gt;:&lt;/label&gt;
          &lt;form:input path="realName" id="emailRealName"/&gt;
          &lt;form:errors path="realName" cssClass="error"/&gt;
  
          &lt;label for="emailIsActive"&gt;&lt;spring:message code="email.form.label.IsActive" text="Activation status"/&gt;:&lt;/label&gt;
          &lt;form:checkbox path="active" id="emailIsActive"/&gt;
          &lt;form:errors path="active" cssClass="error"/&gt;
  
          &lt;form:checkboxes path="categories" element="div" items="${categories}" itemValue="id" itemLabel="name"/&gt;
          &lt;form:errors path="categories" cssClass="error"/&gt;
  
          &lt;button type="submit"&gt;&lt;spring:message code="_common.form.Submit" text="Save"/&gt;&lt;/button&gt;
      &lt;/fieldset&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p><strong>Note:</strong> I don't want multiple <code>@ModelAttribute</code>s to come from POST, just want to distinguish somehow form model from previously generated attribute(s).</p>