dijit/form/Form
contains a few utilities that work with Dijit form widgets (dijit/form/TextBox
, dijit/form/Select
, ...). I don't know if you're using those or not, but the answer is quite similar with or without. The idea is t oserialize your form to an object and to submit that form using AJAX calls.
With Dijit form widgets
If you use the Dijit form widgets, you can serialize the entire form using getValues()
on your form widget. This will serialize the form for you:
xhr.post("mySubmitUrl", {
data: registry.byId("myForm").getValues()
}).then(function() {
console.log("Success");
});
I also made a JSFiddle to demonstrate this.
With native form elements
If you're using native form elements, you have no advantage of using dijit/form/Form
. To serialize the form you can use the function toObject()
from the dojo/dom-form
module. This function requires you to pass a form DOM node.
This means you can either pass the domnode of your dijit/form/Form
widget, or entirely skip this part and just access the DOM node directly.
xhr.post("mySubmitUrl", {
data: domForm.toObject(registry.byId("myForm").domNode)
}).then(function() {
console.log("Success");
});
An example JSFiddle can be found here.
Now on the server-side (Spring), you just add a method that will handle that specific request, for example:
@RequestMapping(value = "mySubmitUrl", method = RequestMethod.POST)
public void doPostForm(@RequestParam("name") String name, @RequestParam("firstname") String firstname) {
// Do stuff
}