title: Spring MVC Annotation Based Controller command object losing values on POST
body: <p>I am loading a user object my calling a service and then store this user as a command object in the model on GET in the controller.  This user object has many properties that are not mapped in the jsp page.  After submitting the form, I am getting the command object i the controller on POST.  But strangely, I only see the properties in the command object which are mapped to the jsp page.  All the other properties those were there when I load the object are lost.  I need all the properties in object to be able to successfully save it in hte database.</p>
  
  <p>Can anybody help me figure this problem?  Thanks!</p>
  
  <p><strong>Update</strong></p>
  
  <p>I am adding some code to better understand it.  In POST handler, I was expecting the command object to have all the properties that was loaded in GET handler in addition to the properties that are bound with jsp.  Instead I am losing all propeties except those are bound to the jsp.  Am I doing something wrong here?      </p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET)
      public String showForm(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception {
          UserBean user = Util.getUser(session);
          UserBean command = (UserBean)userProfileService.loadByUserName(user.getUserName());
          model.addAttribute("command", command);
          return formView;
      }
  
  @RequestMapping(method = RequestMethod.POST)
      public String onSubmit(@ModelAttribute("command") UserBean command, BindingResult result, HttpSession session) throws Exception {
       UserBean user = (UserBean) command;
           userProfileService.saveUser(user);
           return "successView";
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I am adding some code to better understand it. In POST handler, I was expecting the command object to have all the properties that was loaded in GET handler in addition to the properties that are bound with jsp. Instead I am losing all propeties except those are bound to the jsp. Am I doing something wrong here?</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET) public String showForm(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception { UserBean user = Util.getUser(session); UserBean command = (UserBean)userProfileService.loadByUserName(user.getUserName()); model.addAttribute("command", command); return formView; }
  
  @RequestMapping(method = RequestMethod.POST) public String onSubmit(@ModelAttribute("command") UserBean command, BindingResult result, HttpSession session) throws Exception { UserBean user = (UserBean) command; userProfileService.saveUser(user); return "successView"; }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>If I store the command object in session how would the jsp bind the propeties. I thought I needed to store it in model for that?
  Could you explain please.</p>
  
  <p><strong>Update</strong></p>
  
  <p>storing the command object in session solves the problem. I was able to store it by using</p>
  
  <pre><code>@SessionAttributes ("command")
  </code></pre>
  
  <p>Thanks a lot!</p>