Simple autosave for Spring MVC form
-
05-09-2019 - |
Question
I have one page form done with Spring MVC. User has to register first and I would like the form to be saved automatically so user can come later to finalize it. Traditional form submission with a timer is not what I'm looking for. Instead, I am planning to use some ajaxy way to send fields to the server at each onFocus event.
Any suggestions for implementations or comments for this approach?
Solution
I just recently did the same thing. I used JQuery and used their "serialize" function along with their "ajax" function to do the form submission. In my case, rather than use an event-driven approach, I simply saved the serialized form to a variable and checked once a minute to see if a newly serialized form was different from the previously saved one.
OTHER TIPS
you also can check XT Ajax framework
I solved this using http://docs.jquery.com/Ajax/serialize as JacobM suggested. The serialized form is then sent as ajax post triggered by a timer.
In Spring MVC I created a very basic controller extending AbstractController and extracted my fields from request.
Below Js script will help you to make ajax call when ever form field changes.
<script>
$(document).ready($('.form-control').change(function() {
$.ajax({
type : "post",
url : "http://localhost:8521/SpringExamples/autosave/save.htm",
cache : false,
data : $('#employeeForm').serialize(),
success : function(response) {
var obj = JSON.parse(response);
$("#alert").text(JSON.stringify(obj));
$("#alert").addClass("alert-success");
},
error : function() {
alert('Error while request..');
}
});
}));
</script>