Spring MVC Submit Form jQuery
In this tutorial you will see how to submit Spring MVC application form data using jQuery. Please follow steps below:
- Download and unzip code and import as maven project into eclipse.
- Project structure:
- SubmitForm.jsp:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring MVC submit form AJAX JQuery</title> <script src="http:////code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#submitId').click(function() { $( "#submitForm" ).submit(); }); }); </script> </head> <body> <form:form commandName="user" action="submitForm.web" method="post" id="submitForm"> <fieldset style="width: 300px;"> <legend>User details</legend> <ol> <li> <p><label for=name>Name</label> <form:input path="name" type="text" placeholder="First and last name" /></p> </li> <li> <p><label for=name>Date</label> <form:input path="dob" type="date" required="true"/></p> </li> <li> <p><label for=email>Email</label> <form:input path="email" type="text" required="true" /></p> </li> <li> <p><label for=phone>Phone</label> <form:input path="phone" type="text" required="true" /></p> </li> </ol> </fieldset> <fieldset style="width: 300px;"> <input id="submitId" type="button" value="Submit Form"> </fieldset> </form:form> </body> </html>
- SpringMVCController.java:
package com.javahonk.controller; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class SpringMVCController { private static final Logger logger = Logger.getLogger(SpringMVCController.class); @RequestMapping(value = "/helloWorld.web", method = RequestMethod.GET) public ModelAndView printWelcome(@ModelAttribute("user") Users user) { ModelAndView mav = new ModelAndView("SubmitForm"); mav.addObject("message", "Hello World!!!"); return mav; } @RequestMapping(value="/submitForm.web", method = RequestMethod.POST) public ModelAndView submittedFromData(@ModelAttribute("user") Users user) { logger.debug("submitted FromData mehtod called"); ModelAndView mav = new ModelAndView("SubmitForm"); return mav; } }
- Application output:
- For more information about jQuery form submit please use this link
Download Project: SpringMVCAJAXFormSubmit
I used this but it is not working with spring 3. when i am using @ModelAttribute then only it’s calling Controller method but when i am using @RequestBody then it’s not working
so what i should do?
Is there any reason you want to use Spring 3 in place of 4.0.6? Those are not compatible with version 3.