title: Spring Framework JSP Form Handling: @ModelAttribute
body: <p>I have recently started working with JSPs in Spring framework and I tried implementing a form handling page. </p>
  
  <p>I created a basic form page that takes user input and after they hit submit they receive a confirmation page with the input that they entered. </p>
  
  <p>These are the two mapped methods I have in my controller class: </p>
  
  <pre><code>import java.util.Map;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  
  import com.redcup.model.Person;
  import com.redcup.model.User;
  import com.redcup.test.ServletTestBean;
  
  import javax.annotation.PostConstruct;
  
  @Controller
  public class RedCupController {
  
      private final Logger logger = LoggerFactory.getLogger(RedCupController.class);
  
      @PostConstruct
      public void intializeComplete() { 
          logger.info("Controller initialized");
      }
  
      @RequestMapping(method=RequestMethod.GET, value="/health")
      @ResponseBody
      public String getHealth()
      {
          logger.info("GET /health invoked.");
  
          return "hello world!";
      }
  
      @RequestMapping(method=RequestMethod.GET, value="/welcome")
      public String getWelcome(Model model) { 
          User user = new User();
          model.addAttribute("userform", user);
          return "welcome"; 
      }
  
      @RequestMapping(method=RequestMethod.POST, value="/confirmation")
      public String getConfirmation(@ModelAttribute("userform") User user, @ModelAttribute("pew") User user2, 
                                  @ModelAttribute("whomp")Person person, Model model) {
  
          user = getUserForm();
          user2 = getUserForm2();
  
          System.out.println("hello: " + user);
          System.out.println("goodbye: " + user2);
  
          System.out.println("user: " + user.getFirstName());
          System.out.println("user: " + user.getLastName());
          System.out.println("user: " + user.getEmail());
          System.out.println("user: " + user.getAge());
  
          System.out.println("user2: " + user2.getFirstName());
          System.out.println("user2: " + user2.getLastName());
          System.out.println("user2: " + user2.getEmail());
          System.out.println("user2: " + user2.getAge());
  
          System.out.println("person: " + person.getName());
          System.out.println("person: " + person.getNum2());
  
          model.addAttribute("user", user);
          model.addAttribute("user2", user2);
          model.addAttribute("person", person);
  
          return "confirmation";
      }
  
      @ModelAttribute("userform")
      public User getUserForm() {
          return new User();
      }
  
      @ModelAttribute("userform2") 
      public User getUserForm2() { 
          return new User();
      }
  }
  </code></pre>
  
  <p>The User class for the model: </p>
  
  <p>public class User {</p>
  
  <pre><code>private String firstName;
  private String lastName;
  private String email;
  private int age; 
  
  public User() { 
      firstName = "John";
      lastName =  "Smith";
      email = "default email";
      age = 1; 
  }
  
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }
  
  public String getFirstName () { 
      return firstName;
  }
  
  public void setLastName(String lastName) { 
      this.lastName = lastName;
  }
  
  public String getLastName() { 
      return lastName;
  }
  
  public void setEmail(String email) { 
      this.email = email;
  }
  
  public String getEmail() { 
      return email;
  }
  
  public void setAge (int age) {
      this.age = age;
  }
  
  public int getAge() { 
      return age;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And below is the form entry page welcome.jsp: </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div align="center"&gt;
  
          &lt;form:form action="confirmation" method="post" commandName="userform"&gt;
              &lt;form:input path="firstName"/&gt;
              &lt;form:input path="lastName"/&gt;
              &lt;form:input path="email"/&gt;
              &lt;form:input path="age"/&gt;
              &lt;input type="submit" value="Press this to submit"/&gt;
          &lt;/form:form&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>In my controller handler: getConfirmation I have two User parameters (user and user2).The part I am confused about is that when I click submit in my welcome.jsp, user and user2 are populated with the same exact values from the Model in the welcome.jsp. I'm not sure why this happens as I thought that the @ModelAttribute will only assign the corresponding model attribute to the specified parameter (i.e. user will receive the model under the commandName userform). </p>
  
  <p>But to me, it doesn't seem like the @ModelAttribute annotation is doing anything. The user and user2 parameters will be populated with the same field values (what I entered into the form) when the @ModelAttributes have different keys (i.e. userform vs userform2) and when I even remove the @ModelAttribute annotations, user and user2 will STILL be popoulated with the same field values. </p>
  
  <p>Can someone explain to why this is occurring and what exactly the @ModelAttribute annotation does? </p>
  
  <p>Thanks in advance! </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have created a UserWrapper class per Alan's Hay's suggestion: </p>
  
  <p>public class UserWrapper {</p>
  
  <pre><code>    private User user1;
      private User user2; 
  
      public User getUser1() { 
          return user1;
      }
  
      public User getUser2() { 
          return user2; 
      }
  
      public void setUser1(User user) { 
          user1 = user;
      }
  
      public void setUser2(User user) { 
          user2 = user;
      }
  
      public void setUser1FirstName(String firstName) {
          user1.setFirstName(firstName);
      }
  
      public String getUser1FirstName() { 
          return user1.getFirstName();
      }
  
      public void setUser2FirstName(String firstName) {
          user2.setFirstName(firstName);
      }
  
      public String getUser2FirstName() { 
          return user2.getFirstName();
      }
  }
  </code></pre>
  
  <p>and updated my welcome.jsp with the following form: </p>
  
  <pre><code>    &lt;form:form action="confirmation" method="post" commandName="userform"&gt;
          &lt;form:input path="user1.firstName"/&gt;
          &lt;form:input path="user1.lastName"/&gt;
          &lt;form:input path="user1.email"/&gt;
          &lt;form:input path="user1.age"/&gt;
          &lt;input type="submit" value="Press this to submit"/&gt;
      &lt;/form:form&gt;
  </code></pre>