{"id":940,"date":"2010-11-25T15:12:51","date_gmt":"2010-11-25T14:12:51","guid":{"rendered":"http:\/\/sahits.ch\/blog\/?p=940"},"modified":"2010-11-30T13:08:11","modified_gmt":"2010-11-30T12:08:11","slug":"spring-mvc-forms-with-select-drop-down-list","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2010\/11\/25\/spring-mvc-forms-with-select-drop-down-list\/","title":{"rendered":"Spring MVC forms with select drop down list"},"content":{"rendered":"<p>I&#8217;m currently developing a Spring MVC application that uses forms. Some of the form elements provided are easy enough to use with a backing model (such as simple input fields (text, password, textarea)). With the selection list however I had problems since I could not work out how to populate the form from the model and how to handle the submit event. All this is probably caused by the complexity of the problem domain and the used model. Therefore I set out and implemented an easy example. There are some articles on how to do that but none with the Annotation process introduced since Spring 2.5. My example is based on this article <a href=\"http:\/\/www.mkyong.com\/spring-mvc\/spring-mvc-dropdown-box-example\/\">Spring MVC dropdown example<\/a>.<!--more--><br \/>\nThe main focus of this article is the contoller part because that is the only difference between the example before 2.5 and this one. The JSP part is mainly the same only that I added the action <code>\/test<\/code> to the form.<br \/>\nThe model I used is exactly the same:<\/p>\n<pre>\r\npublic class Customer{\r\n\t \r\n\tString country;\r\n\tString javaSkills;\r\n \r\n\tpublic String getCountry() {\r\n\t\treturn country;\r\n\t}\r\n\tpublic void setCountry(String country) {\r\n\t\tthis.country = country;\r\n\t}\r\n\tpublic String getJavaSkills() {\r\n\t\treturn javaSkills;\r\n\t}\r\n\tpublic void setJavaSkills(String javaSkills) {\r\n\t\tthis.javaSkills = javaSkills;\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<p>Now for the interesting part &#8211; the controller &#8211; I named it TestController, the name of the controller class is irrelevant. What is important is that it is annotated with <code>@Controller<\/code> and <code>@RequestMapping<\/code>.<\/p>\n<pre>\r\n@Controller\r\n@RequestMapping\r\npublic class TestController {\r\n...\r\n<\/pre>\n<p>The controller needs two methods:<br \/>\nOne to provide the data when the page is loaded and one that handles the submission of the form.<br \/>\nAll data used in the JSP page is stored in a ModelMap that is stored in the PageContext. This is done by a method that is annotated with part of the URI. The ModelMap that is passed as an argument to this method:<\/p>\n<pre>\r\n@RequestMapping(value = \"\/test\", method = RequestMethod.GET)\r\npublic String provide(ModelMap modelMap){\r\n<\/pre>\n<p>Next we put the backing model into the map. The path element in the JSP page corresponds to the property in the backing model. If a property has a value this is a preselected value.<\/p>\n<pre>\r\nCustomer cust = new Customer();\r\ncust.setJavaSkills(\"Spring\");\r\nmodelMap.addAttribute(\"customerForm\", cust);\r\n<\/pre>\n<p>The key under which the backing model is put into the ModelMap is the same as the commandName in the JSP. Next we need to define the options for the selections. This is done as a Map. the keys of the map correspond the the value attribute of the option element, the value is the displayed value.<\/p>\n<pre>\r\nMap&lt;String,String&gt; country = new LinkedHashMap&lt;String,String&gt;();\r\ncountry.put(\"US\", \"United Stated\");\r\ncountry.put(\"CHINA\", \"China\");\r\ncountry.put(\"SG\", \"Singapore\");\r\ncountry.put(\"MY\", \"Malaysia\");\r\nmodelMap.put(\"countryList\", country);\r\n \r\nMap&lt;String,String&gt; javaSkill = new LinkedHashMap&lt;String,String&gt;();\r\njavaSkill.put(\"Hibernate\", \"Hibernate\");\r\njavaSkill.put(\"Spring\", \"Spring\");\r\njavaSkill.put(\"Apache Wicket\", \"Apache Wicket\");\r\njavaSkill.put(\"Struts\", \"Struts\");\r\nmodelMap.put(\"javaSkillsList\", javaSkill);\r\n<\/pre>\n<p>The method requires a String that represents that page to be loaded. This can either by a hard coded JSP page or a placeholder that is configured in a view.xml. <\/p>\n<pre>\r\nreturn \"config_test\";\r\n<\/pre>\n<pre>\r\n     &lt;definition name=\"config_test\" template=\"\/WEB-INF\/views\/portal\/test_user.jspx\"\/&gt;\r\n<\/pre>\n<p>For details on how to configure your application to work with view.xml see the <a href=\"http:\/\/static.springsource.org\/spring\/docs\/current\/spring-framework-reference\/html\/mvc.html#mvc-viewresolver\">MVC Reference<\/a>.<br \/>\nThe whole method put together looks like this:<\/p>\n<pre>\r\n@RequestMapping(value = \"\/test\", method = RequestMethod.GET)\r\npublic String provide(ModelMap modelMap){\r\n\tCustomer cust = new Customer();\r\n\tcust.setJavaSkills(\"Spring\");\r\n\tmodelMap.addAttribute(\"customerForm\", cust);\r\n\t\r\n\tMap&lt;String,String&gt; country = new LinkedHashMap&lt;String,String&gt;();\r\n\tcountry.put(\"US\", \"United Stated\");\r\n\tcountry.put(\"CHINA\", \"China\");\r\n\tcountry.put(\"SG\", \"Singapore\");\r\n\tcountry.put(\"MY\", \"Malaysia\");\r\n\tmodelMap.put(\"countryList\", country);\r\n \r\n\tMap&lt;String,String&gt; javaSkill = new LinkedHashMap&lt;String,String&gt;();\r\n\tjavaSkill.put(\"Hibernate\", \"Hibernate\");\r\n\tjavaSkill.put(\"Spring\", \"Spring\");\r\n\tjavaSkill.put(\"Apache Wicket\", \"Apache Wicket\");\r\n\tjavaSkill.put(\"Struts\", \"Struts\");\r\n\tmodelMap.put(\"javaSkillsList\", javaSkill);\r\n\t\r\n\treturn \"config_test\";\r\n}\r\n<\/pre>\n<p>The method to handle the submission has a slightly different signature:<\/p>\n<pre>\r\n\t@RequestMapping(value = \"\/test\", method = RequestMethod.POST)\r\n\tpublic String save(@ModelAttribute(\"customerForm\") Customer customer, ModelMap modelMap){\r\n<\/pre>\n<p>The request method is POST since the method in the form is POST. The model is used with the annotation <code>@ModelAttribute<\/code> and the value of the commandName. The customer instance represents the data as selected in the form. The rest of the method is straight forward and left as an exercise to the dedicated reader.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m currently developing a Spring MVC application that uses forms. Some of the form elements provided are easy enough to use with a backing model (such as simple input fields (text, password, textarea)). With the selection list however I had problems since I could not work out how to populate the form from the model &hellip; <a href=\"http:\/\/sahits.ch\/blog\/blog\/2010\/11\/25\/spring-mvc-forms-with-select-drop-down-list\/\" class=\"more-link\"><span class=\"screen-reader-text\">\u201eSpring MVC forms with select drop down list\u201c <\/span>weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6],"tags":[165,113,164,162,301,163],"class_list":["post-940","post","type-post","status-publish","format-standard","hentry","category-java","category-programmieren","tag-drop-down","tag-en","tag-form","tag-html","tag-java","tag-spring-3-0"],"_links":{"self":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/comments?post=940"}],"version-history":[{"count":4,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/940\/revisions"}],"predecessor-version":[{"id":967,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/940\/revisions\/967"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}