Jquery – How to save Spring form data using jquery ajax without refreshing page


i try out this example… where student class object is getting at controller side. and we can use it directly. but here the page is getting submitted.

here i want it call the post method using jquery ajax and post data as class object. instead of sending each input field value.

how can i achieve this?

for example i want to send post request to this controller

@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      //here will be my code to use student object....

      return "result";

using this Spring form..

<form:form method="POST" action="/HelloWeb/addStudent">
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age" /></td>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
        <td colspan="2">
            <input type="submit" value="Submit"/>

Best Solution

I am going to answer with an existing question.. jQuery AJAX submit form

For Spring's model attribute mapping to work, you only have to make sure the form input names stay consistent. The best way to progressively convert your traditional form submit to an ajax style form submit is to trap the form submit event, serialize the form and make an ajax call. Since all param names and values are going to remain consistent, your backend code will work as is.

Note: You will have to handle responses differently though, since a normal form submit returns the success or error page, while with ajax you'll need something else. But since this question is about form submission, won't go into details about response handling.

Hope this answers your question.