title: Spring MVC ModelAttribute Fields Are Null Instead of Form Input Values
body: <p>I'm trying to make a form that will post a CardRequestResource:</p>
  
  <pre><code>public class CardRequestResource extends ResourceSupport{
      private Long cardRequestId;
      private String deliveryMethod;
      private String address;
      private boolean isHomeDelivery;
      private String requestDate;
      private String expectedDate;
      private String comments;
      private Long statusId;
      private PersonResource person;
  
      //Getters and Setters
  }
  </code></pre>
  
  <p>In my controller, I first load the JSP and add an empty CardRequestResource to the ModelMap:</p>
  
  <pre><code>@RequestMapping(value = { "", "/approval" }, method = RequestMethod.GET)
  public String getApproval(ModelMap map) {
      map.put("cardRequestResource", new CardRequestResource());       
      return "cardoffice/approval";
  }
  </code></pre>
  
  <p>My JSP builds the form with the cardRequestResource model attribute:</p>
  
  <pre><code>&lt;form:form id="detailForm" name="detailForm" modelAttribute="cardRequestResource"&gt;
      &lt;form:input path="statusId" type="hidden" id="statusId" name="statusId" /&gt;
      &lt;form:textarea path="comments" name="comments" id="commentTextarea" rows="7" cols="81" style="font-style: normal;"/&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>A Javascript function makes an AJAX call to populate the form values:</p>
  
  <pre><code>function getCardRequestDetails(cardRequestUrl) {
      $.ajax({
          type : "GET",
          url : cardRequestUrl,
          dataType : "json",
          success : function(response) {
              loadCardRequestDetails(response);
          },
          error : function(response) {}
      });
  };
  
  function loadCardRequestDetails(cardRequest) {
      $("#statusId").attr("value", cardRequest.statusId);
      $("#commentTextarea").val(cardRequest.comments);
  }
  </code></pre>
  
  <p>At this point a user may update the comment text area, and the hidden input may change conditionally on what the user enters in the field. Then when the form is submitted, I call the following Javascript function:</p>
  
  <pre><code>function postCardRequest(url) {
      var serialized = $("#detailForm").serialize();
      alert(serialized);
      $.ajax({
          type: "POST",
          url: url,
          data: serialized,
          contentType: "application/json",
          dataType: "json"                
      });
  }
  </code></pre>
  
  <p>The alert shows that the fields are populated correctly with the data that was either originally loaded by AJAX/Javascript, or by the user. However when I get to the handler in my controller that processes the post, the CardRequestResource is non-null, but EVERY SINGLE field in it is NULL!</p>
  
  <p>Handler code:</p>
  
  <pre><code>@RequestMapping(value = "/approval/submit", method = RequestMethod.POST)
  public @ResponseBody Map&lt;String, Object&gt; postCardRequest(@ModelAttribute(value = "cardRequestResource") CardRequestResource cardRequestResource) {
      Map&lt;String, Object&gt; responseMap = new HashMap&lt;String, Object&gt;();
      final String success = "success";
      Boolean isSuccessful = Boolean.FALSE;
  
      if(cardRequestResource != null){
          isSuccessful = Boolean.TRUE;
      }
      //TODO
  
      System.out.println("Status: " + cardRequestResource.getStatusId() + ", Comments: " + cardRequestResource.getComments());
  
      responseMap.put(success, isSuccessful);
      return responseMap;
  }
  </code></pre>