1

How to pass form parameters into spring controller. I need to submit the below form into this url "submit.htm".

<form data-bind="submit: save" action="forms/submit.htm" method="post">
    <fieldset>
        <legend>User: <span data-bind='text: errors().length'></span> errors</legend>
        <label>First name: <input data-bind='value: firstName'/></label>
        <label>Last name: <input data-bind='value: lastName'/></label>
    </fieldset>
    <button  type="submit">Go</button>
</form>

JS:

var viewModel = {
    firstName: ko.observable().extend({
        required: true,
        minLength: 2,
        maxLength: 10
    }),
    lastName: ko.observable().extend({
        required: true
    }),
    tasks: ko.observableArray([]),
    save: function() {
        if (viewModel.errors().length == 0) {
            ko.utils.postJson($("form")[0], this);
        } else {
            alert('Please check your submission.');
            viewModel.errors.showAllMessages();
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);
ko.applyBindings(viewModel);

this is going to my controller. But I didnot get those parameters in my controller. Also I dont know the way to get these params in my controller.

nickdos
  • 8,348
  • 5
  • 30
  • 47
Prasath
  • 1,233
  • 2
  • 16
  • 38
  • Does the `ko.utils.postJson()` send the form data as JSON in the request body or as request parameters? – nickdos Nov 29 '12 at 04:19
  • Thanks for your response nickdos. That is what I dont know – Prasath Nov 29 '12 at 09:52
  • You should be able to use Firebug or Chrome developer tools to inspect the Ajax request in the browser to see which it is sending - even without a functioning end point. – nickdos Nov 29 '12 at 10:06
  • Yes Nickdos, my data is passing to controller. But I don't know how to receive in my spring controller. We need to submit form as ko.utils.postJson($("form")[0], {userList : {firstName:this.firstName(),lastName:this.lastName()}}); – Prasath Nov 30 '12 at 07:46
  • Please check http://stackoverflow.com/a/25397899/2182503 – Tunguska Aug 20 '14 at 06:32

0 Answers0