{"id":11590,"date":"2013-04-23T06:45:32","date_gmt":"2013-04-23T03:45:32","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=11590"},"modified":"2013-04-23T06:45:32","modified_gmt":"2013-04-23T03:45:32","slug":"spring-mvc-form-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html","title":{"rendered":"Spring MVC Form Tutorial"},"content":{"rendered":"<p>This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You can grab the code <a title=\"Spring MVC Form Source Code\" href=\"https:\/\/github.com\/stevehanson\/spring-mvc-form\" target=\"_blank\">on GitHub<\/a>.<\/p>\n<h2>Prerequisites:<\/h2>\n<p>You should have a working Spring MVC Application. If you do not already have a working Spring MVC application set up, follow <a title=\"Spring MVC Web Application Tutorial\" href=\"http:\/\/codetutr.com\/2013\/03\/24\/simple-spring-mvc-web-application-using-gradle\/\">this tutorial<\/a>. For this tutorial, we are going to make a simple form for subscribing to a newsletter. The form will have the following fields:<\/p>\n<ul>\n<li>name \u2013 input field<\/li>\n<li>age \u2013 input field<\/li>\n<li>email \u2013 input field<\/li>\n<li>gender \u2013 select drop-down<\/li>\n<li>receiveNewsletter \u2013 checkbox<\/li>\n<li>newsletterFrequency \u2013 select drop-down<\/li>\n<\/ul>\n<h2>Requirements:<\/h2>\n<ul>\n<li>The <code>newsletterFrequency<\/code> drop-down should only be active if the <code>receiveNewsletter<\/code> checkbox is checked<\/li>\n<li>We will not be performing any validations in this example (stay-tuned for future tutorial)<\/li>\n<li>When the user submits the form, the same page will reload<\/li>\n<li>Reloaded page should display a message that indicates that the submission was successful and shows the saved values<\/li>\n<\/ul>\n<p>When we\u2019re done, we will have a page that looks like this:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/spring-mvc-form-screenshot.png\"><img decoding=\"async\" class=\"aligncenter\" alt=\"Spring MVC form screenshot\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/spring-mvc-form-screenshot.png\" width=\"550\" height=\"433\" \/><\/a><\/p>\n<p>First, let\u2019s set up the object we will use to store the subscriber\u2019s information. Create the class <code>Subscriber<\/code> in package <code>com.codetutr.form<\/code>. This is a basic Java bean. Notice we are using enumerations to store the gender and newsletter frequency fields. For simplicity, I defined the enums in the same class. Also notice that we are defining the <code>toString<\/code>. This is just so we can easily get the values to print after submission.<\/p>\n<p><code>Subscriber.java<\/code><\/p>\n<pre class=\" brush:java\">package com.codetutr.form;\r\n\r\npublic class Subscriber {\r\n\r\n\tprivate String name;\r\n\tprivate String email;\r\n\tprivate Integer age;\r\n\tprivate Gender gender;\r\n\tprivate Frequency newsletterFrequency;\r\n\tprivate Boolean receiveNewsletter;\r\n\r\n\tpublic enum Frequency {\r\n\t\tHOURLY, DAILY, WEEKLY, MONTHLY, ANNUALLY\r\n\t}\r\n\r\n\tpublic enum Gender {\r\n\t\tMALE, FEMALE\r\n\t}\r\n\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\r\n\r\n\tpublic String getEmail() {\r\n\t\treturn email;\r\n\t}\r\n\r\n\tpublic void setEmail(String email) {\r\n\t\tthis.email = email;\r\n\t}\r\n\r\n\tpublic Integer getAge() {\r\n\t\treturn age;\r\n\t}\r\n\r\n\tpublic void setAge(Integer age) {\r\n\t\tthis.age = age;\r\n\t}\r\n\r\n\tpublic Gender getGender() {\r\n\t\treturn gender;\r\n\t}\r\n\r\n\tpublic void setGender(Gender gender) {\r\n\t\tthis.gender = gender;\r\n\t}\r\n\r\n\tpublic Frequency getNewsletterFrequency() {\r\n\t\treturn newsletterFrequency;\r\n\t}\r\n\r\n\tpublic void setNewsletterFrequency(Frequency newsletterFrequency) {\r\n\t\tthis.newsletterFrequency = newsletterFrequency;\r\n\t}\r\n\r\n\tpublic Boolean getReceiveNewsletter() {\r\n\t\treturn receiveNewsletter;\r\n\t}\r\n\r\n\tpublic void setReceiveNewsletter(Boolean receiveNewsletter) {\r\n\t\tthis.receiveNewsletter = receiveNewsletter;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic String toString() {\r\n\t\treturn \"Subscriber [name=\" + name + \", age=\" + age + \", gender=\" + gender\r\n\t\t\t\t+ \", newsletterFrequency=\" + newsletterFrequency\r\n\t\t\t\t+ \", receiveNewsletter=\" + receiveNewsletter + \"]\";\r\n\t}\r\n\r\n}<\/pre>\n<p>Now, let\u2019s create the controller. Create class <code>FormController<\/code> in package <code>com.codetutr.controller<\/code>:<\/p>\n<p><code>FormController.java<\/code><\/p>\n<pre class=\" brush:java\">package com.codetutr.controller;\r\n\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.ui.Model;\r\nimport org.springframework.web.bind.annotation.ModelAttribute;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestMethod;\r\n\r\nimport com.codetutr.form.Subscriber;\r\nimport com.codetutr.form.Subscriber.Frequency;\r\n\r\n@Controller\r\npublic class FormController {\r\n\r\n\t@ModelAttribute(\"frequencies\")\r\n\tpublic Frequency[] frequencies() {\r\n\t\treturn Frequency.values();\r\n\t}\r\n\r\n\t@RequestMapping(value=\"form\", method=RequestMethod.GET)\r\n\tpublic String loadFormPage(Model m) {\r\n\t\tm.addAttribute(\"subscriber\", new Subscriber());\r\n\t\treturn \"formPage\";\r\n\t}\r\n\r\n\t@RequestMapping(value=\"form\", method=RequestMethod.POST)\r\n\tpublic String submitForm(@ModelAttribute Subscriber subscriber, Model m) {\r\n\t\tm.addAttribute(\"message\", \"Successfully saved person: \" + subscriber.toString());\r\n\t\treturn \"formPage\";\r\n\t}\r\n}<\/pre>\n<p>Let\u2019s look at a few things in the code above. First, notice that both request handlers (methods annotated with <code>@RequestMapping<\/code>) are mapped to the same URL \u2013 \u201cform\u201d. The only difference in the mapping is that one handles an HTTP GET request, and the other a POST. The first handler (for the GET request) will be invoked when the user navigates to the \u201cform\u201d page, because they will access the page using a GET request. The POST handler is invoked when the form is submitted (since it will be submitted via HTTP POST to the \u201cform\u201d URL). You could, of course, submit your form to any URL using any HTTP method \u2013 just make sure to map your handler accordingly here.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Let\u2019s look at the GET handler. It takes a <code>Model<\/code>, which we populate with an empty <code>Subscriber<\/code> object. This object is what we will use to populate our form. We are not setting any values here, but if we wanted to, say default the <code>receiveNewsletter<\/code> checkbox to true and set default newsletter frequency to hourly, we could do:<\/p>\n<pre class=\" brush:java\">Subscriber subscriber = new Subscriber();\r\nsubscriber.setReceiveNewsletter(true);\r\nsubscriber.setNewsletterFrequency(Frequency.HOURLY);\r\nm.addAttribute(\"subscriber\", subscriber);<\/pre>\n<p>Also note that if we do not add an object called \u201csubscriber\u201d to the model, Spring would complain when we try to access the JSP, because we will be setting up the JSP to bind the form to the \u201csubscriber\u201d model attribute. You would see a JSP error: \u201cNeither BindingResult nor plain target object for bean name \u2018subscriber\u2019 available as request attribute\u201d and the JSP would not render.<\/p>\n<p>The last thing to look at in the controller code is the <code>@ModelAttribute<\/code> method. When a method is annotated with <code>@ModelAttribute<\/code>, Spring runs it before each handler method and adds the return value to the model. We specified in the annotation to add the Frequency values to the model as \u201cfrequencies\u201d. This object will be used to populate the newsletter frequency drop-down box in the JSP form. Instead of using the @ModelAttribute method, we could have added the following line to each of the request handlers:<\/p>\n<pre class=\" brush:java\">m.addAttribute(\"frequencies\", Frequency.values())<\/pre>\n<p>Finally, let\u2019s set up the jsp. Create a file called <code>formPage.jsp<\/code> in <code>WEB-INF\/view<\/code> (or wherever you have configured your JSPs to reside):<\/p>\n<p><code>formPage.jsp<\/code><\/p>\n<pre class=\" brush:xml\">&lt;%@ taglib prefix=\"c\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" %&gt;\r\n&lt;%@ taglib prefix=\"form\" uri=\"http:\/\/www.springframework.org\/tags\/form\" %&gt;\r\n\r\n&lt;!DOCTYPE HTML&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;Sample Form&lt;\/title&gt;\r\n    &lt;script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.9.1\/jquery.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;style&gt;\r\n      body { background-color: #eee; font: helvetica; }\r\n      #container { width: 500px; background-color: #fff; margin: 30px auto; padding: 30px; border-radius: 5px; box-shadow: 5px; }\r\n      .green { font-weight: bold; color: green; }\r\n      .message { margin-bottom: 10px; }\r\n      label {width:70px; display:inline-block;}\r\n      form {line-height: 160%; }\r\n      .hide { display: none; }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n\r\n  &lt;div id=\"container\"&gt;\r\n\r\n    &lt;h2&gt;Subscribe to The Newsletter!&lt;\/h2&gt;\r\n    &lt;c:if test=\"${not empty message}\"&gt;&lt;div class=\"message green\"&gt;${message}&lt;\/div&gt;&lt;\/c:if&gt;\r\n\r\n    &lt;form:form modelAttribute=\"subscriber\"&gt;\r\n      &lt;label for=\"nameInput\"&gt;Name: &lt;\/label&gt;\r\n      &lt;form:input path=\"name\" id=\"nameInput\" \/&gt;\r\n      &lt;br\/&gt;\r\n\r\n      &lt;label for=\"ageInput\"&gt;Age: &lt;\/label&gt;\r\n      &lt;form:input path=\"age\" id=\"ageInput\" \/&gt;\r\n      &lt;br\/&gt;\r\n\r\n      &lt;label for=\"emailInput\"&gt;Email: &lt;\/label&gt;\r\n      &lt;form:input path=\"email\" id=\"emailInput\" \/&gt;\r\n      &lt;br\/&gt;\r\n\r\n      &lt;label for=\"genderOptions\"&gt;Gender: &lt;\/label&gt;\r\n      &lt;form:select path=\"gender\" id=\"genderOptions\"&gt;\r\n        &lt;form:option value=\"\"&gt;Select Gender&lt;\/form:option&gt;\r\n        &lt;form:option value=\"MALE\"&gt;Male&lt;\/form:option&gt;\r\n        &lt;form:option value=\"FEMALE\"&gt;Female&lt;\/form:option&gt;\r\n      &lt;\/form:select&gt;\r\n      &lt;br\/&gt;\r\n\r\n      &lt;label for=\"newsletterCheckbox\"&gt;Newsletter? &lt;\/label&gt;\r\n      &lt;form:checkbox path=\"receiveNewsletter\" id=\"newsletterCheckbox\" \/&gt;\r\n      &lt;br\/&gt;\r\n      &lt;label for=\"frequencySelect\"&gt;Freq:&lt;\/label&gt;\r\n      &lt;form:select path=\"newsletterFrequency\" id=\"frequencySelect\"&gt;\r\n        &lt;form:option value=\"\"&gt;Select Newsletter Frequency: &lt;\/form:option&gt;\r\n        &lt;c:forEach items=\"${frequencies}\" var=\"frequency\"&gt;\r\n          &lt;form:option value=\"${frequency}\"&gt;${frequency}&lt;\/form:option&gt;\r\n        &lt;\/c:forEach&gt;\r\n      &lt;\/form:select&gt;\r\n      &lt;br\/&gt;\r\n\r\n      &lt;br\/&gt;\r\n      &lt;input type=\"submit\" value=\"Submit\" \/&gt;\r\n    &lt;\/form:form&gt;\r\n  &lt;\/div&gt;\r\n\r\n  &lt;script type=\"text\/javascript\"&gt;\r\n\r\n    $(document).ready(function() {\r\n\r\n      toggleFrequencySelectBox(); \/\/ show\/hide box on page load\r\n\r\n      $('#newsletterCheckbox').change(function() {\r\n        toggleFrequencySelectBox();\r\n      })\r\n\r\n    });\r\n\r\n    function toggleFrequencySelectBox() {\r\n      if(!$('#newsletterCheckbox').is(':checked')) {\r\n        $('#frequencySelect').val('');\r\n        $('#frequencySelect').prop('disabled', true);\r\n      } else {\r\n        $('#frequencySelect').prop('disabled', false);\r\n      }\r\n    }\r\n\r\n  &lt;\/script&gt;\r\n\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Let\u2019s walk through the form tags we are using. Notice the line at the top of the page: <code>&lt;%@ taglib prefix=\"form\" uri=\"http:\/\/www.springframework.org\/tags\/form\" %&gt;<\/code>. This imports the Spring Form tags we will be using. When we open the form with the <code>&lt;form:form&gt;<\/code> tag, note that we are specifying the model attribute. This tells Spring to look for an attribute in the Model and bind it to the form. The <code>action<\/code> and <code>method<\/code> attributes can also be specified. If unspecified (as in this example), they default to the current URL and \u201cPOST\u201d, respectively (just like regular HTML forms).<\/p>\n<p>Notice that each of our input fields is using the Spring Form taglib (form: prefix). Each of these fields also specifies a <code>path<\/code> attribute. This must correspond to a getter or setter of the model attribute (in our case, the Subscriber class) according to the standard Java bean convention (get\/is, set prefixed to field name with first letter capitalized). When the page is loaded, the input fields are populated by Spring, which calls the getter of each field bound to an input field. When the form is submitted, the setters are called to save the values of the form to the object.<\/p>\n<p>The <code>&lt;form:input&gt;<\/code> tags are pretty self explanatory. Notice the two instances of <code>&lt;form:select&gt;<\/code> used. In the first select drop-down, for the gender field, notice that we manually list all of the options. In the newsletter frequency select drop-down, though, we loop through the <code>frequencies<\/code> model attribute (remember we added that to the model through the <code>@ModelAttribute<\/code>-annotated method in the Controller) and add each item as an option in the drop-down. Spring automatically will bind the form values to the enums when the form is submitted as long as the value of the selected option is a valid enum name.<\/p>\n<p>When the form is submitted, the POST handler in the controller is invoked. The form is automatically bound to the subscriber argument that we passed in. The <code>@ModelAttribute<\/code> annotation isn\u2019t actually necessary here. I will write more about that in another post.<\/p>\n<p>There you have it! I strongly recommend you download the source and run the code. Post any questions you have in the comments below.<\/p>\n<p><strong>Full Source:<\/strong> <a title=\"Full Source\" href=\"https:\/\/github.com\/stevehanson\/spring-mvc-form\/archive\/master.zip\">ZIP<\/a>, <a title=\"GitHub Repo\" href=\"https:\/\/github.com\/stevehanson\/spring-mvc-form\" target=\"_blank\">GitHub <\/a>To run the code from this tutorial: Must have <a title=\"How to install Gradle\" href=\"http:\/\/codetutr.com\/2013\/03\/23\/how-to-install-gradle\/\">Gradle installed<\/a>. Download the ZIP. Extract. Open command prompt to extracted location. Run gradle jettyRunWar. Navigate in browser to http:\/\/localhost:8080\/form.<\/p>\n<h4>Resources<\/h4>\n<ul>\n<li><a title=\"Spring TagLib Reference\" href=\"http:\/\/static.springsource.org\/spring\/docs\/3.2.x\/spring-framework-reference\/html\/spring-form.tld.html\" target=\"_blank\">Spring Form TagLib Reference Documentation<\/a><\/li>\n<li><a title=\"Spring Form Tutorial\" href=\"http:\/\/www.dzone.com\/tutorials\/java\/spring\/spring-form-tags-1.html\" target=\"_blank\">DZone \u2013 Spring Form Tag Tutorial<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div style=\"border: 1px solid #D8D8D8; background: #FAFAFA; width: 100%; padding-left: 5px;\"><b><i>Reference: <\/i><\/b><a href=\"http:\/\/codetutr.com\/2013\/04\/06\/spring-mvc-form-submission\/\">Spring MVC Form Tutorial<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Steve Hanson at the <a href=\"http:\/\/codetutr.com\/\">CodeTutr<\/a> blog.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You can grab the code on GitHub. Prerequisites: You should have a working Spring MVC Application. If you do not already have a working Spring MVC application set &hellip;<\/p>\n","protected":false},"author":409,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[30,150],"class_list":["post-11590","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-spring","tag-spring-mvc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring MVC Form Tutorial<\/title>\n<meta name=\"description\" content=\"This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring MVC Form Tutorial\" \/>\n<meta property=\"og:description\" content=\"This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-04-23T03:45:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Steve Hanson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Steve Hanson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html\"},\"author\":{\"name\":\"Steve Hanson\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/006867093496e3253c65f37b3fbec162\"},\"headline\":\"Spring MVC Form Tutorial\",\"datePublished\":\"2013-04-23T03:45:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html\"},\"wordCount\":1015,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"Spring\",\"Spring MVC\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html\",\"name\":\"Spring MVC Form Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2013-04-23T03:45:32+00:00\",\"description\":\"This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/spring-mvc-form-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring MVC Form Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/006867093496e3253c65f37b3fbec162\",\"name\":\"Steve Hanson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5f329aad08233a668ee56ee53f005a94376612b242454cfefaf060b5d6474de0?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5f329aad08233a668ee56ee53f005a94376612b242454cfefaf060b5d6474de0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5f329aad08233a668ee56ee53f005a94376612b242454cfefaf060b5d6474de0?s=96&d=mm&r=g\",\"caption\":\"Steve Hanson\"},\"description\":\"Steve is a software developer interested in web development and new technologies. He currently works as a Java consultant at Credera in Dallas, TX.\",\"sameAs\":[\"http:\\\/\\\/codetutr.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/steve-hanson\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring MVC Form Tutorial","description":"This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Spring MVC Form Tutorial","og_description":"This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You","og_url":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-04-23T03:45:32+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","type":"image\/jpeg"}],"author":"Steve Hanson","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Steve Hanson","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html"},"author":{"name":"Steve Hanson","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/006867093496e3253c65f37b3fbec162"},"headline":"Spring MVC Form Tutorial","datePublished":"2013-04-23T03:45:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html"},"wordCount":1015,"commentCount":5,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["Spring","Spring MVC"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html","name":"Spring MVC Form Tutorial","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2013-04-23T03:45:32+00:00","description":"This tutorial will show how to handle a form submission in Spring MVC. We will define a controller to handle the page load and the form submission. You","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/spring-mvc-form-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring MVC Form Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/006867093496e3253c65f37b3fbec162","name":"Steve Hanson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5f329aad08233a668ee56ee53f005a94376612b242454cfefaf060b5d6474de0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5f329aad08233a668ee56ee53f005a94376612b242454cfefaf060b5d6474de0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f329aad08233a668ee56ee53f005a94376612b242454cfefaf060b5d6474de0?s=96&d=mm&r=g","caption":"Steve Hanson"},"description":"Steve is a software developer interested in web development and new technologies. He currently works as a Java consultant at Credera in Dallas, TX.","sameAs":["http:\/\/codetutr.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/steve-hanson"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/409"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=11590"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11590\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=11590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=11590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=11590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}