简体   繁体   中英

Spring MVC @ModelAttribute Not Binding Form

I am trying to POST to a Spring MVC controller method via ajax. Using Chrome's developer tools, I can tell that the values from the form are being sent, but in the method, the form values are null.

Here is the jquery call:

var form = $('#renegotiationForm').serialize();
$.ajax({

    method:'POST', 
    url:'/renegotiate/wizard/startRenegotiation', 
    data:{'renegotiationForm': form}, 
    success: function(data) { this.transitionTo(data); }

});

Here is the Spring MVC method (meant to return only a single string):

@RequestMapping(value="wizard/startRenegotiation", method = RequestMethod.POST)
public @ResponseBody String processStart(@ModelAttribute("renegotiationForm") RenegotiationForm form, BindingResult bindingResult) {

log.debug("Entered showStart(), method=POST");

RenegotiationType type = RenegotiationType.valueOf(form.getRenoType().trim().toUpperCase());
RenegotiationActivity activity = RenegotiationActivity.valueOf(form.getRenoActivity().trim().toUpperCase());

String result = "";
if (type == RenegotiationType.TYPE1 && activity == RenegotiationActivity.ACTIVITY1) {
    result = "deleteType1";
}

return result;
}

The values are bound using the Spring Form taglib, and I have confirmed that the path attributes of the form tags match the fields of the RenegotiationForm.

I think it's because you are trying to send an "string" from ajax and you want to get and Object (RenegotiationForm), try to change it to String and Format in Server-side. I recommend you to add the type you are sending from client-side, too.

@RequestMapping(value = "wizard/startRenegotiation", method = RequestMethod.POST, produces="application/json")

Found the answer. Further on in my code, I had a function like this:

var ajaxcall = function() { $.ajax({
     // ajax settings
    });
}

Unfortunately, setting it up this way doesn't make it work as a jquery deferred, and specifically I couldn't use the .then() function to process of ajax requests.

Thanks for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM