{"id":945,"date":"2010-11-26T13:32:18","date_gmt":"2010-11-26T12:32:18","guid":{"rendered":"http:\/\/sahits.ch\/blog\/?p=945"},"modified":"2010-11-30T13:07:56","modified_gmt":"2010-11-30T12:07:56","slug":"generifing-mvc-spring-select-lists","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2010\/11\/26\/generifing-mvc-spring-select-lists\/","title":{"rendered":"Generifing MVC spring select lists"},"content":{"rendered":"<p>In my <a href=\"http:\/\/sahits.ch\/blog\/?p=940\">previous article<\/a> I have shown how to create a simple form with two select boxes using spring form from MVC. When we have an application with may forms we want to be able to use a general model for backing the data. We also might want to have certain elements multiple times, e.g. not one Customer but several. We will take theses step by step.<!--more--><\/p>\n<h2>Multiple Customers<\/h2>\n<p>The first step is to change the backing model to a list and iterate over all the elements.<\/p>\n<h3>Creating the backing model<\/h3>\n<p>This step is rather straight forward: Create a class that contains a List of type Customer and add the appropriate getter and setter methods:<\/p>\n<pre>\r\npublic class CustomerList{\r\n\tprivate List&lt;Customer&gt; list;\r\n\r\n\tpublic List&lt;Customer&gt; getList() {\r\n\t\treturn list;\r\n\t}\r\n\r\n\tpublic void setList(List&lt;Customer&gt; list) {\r\n\t\tthis.list = list;\r\n\t}\r\n}\r\n<\/pre>\n<h3>Adjusting the controller<\/h3>\n<p>Instead of a Customer instance we put a CustomerList instance into the map. To make the whole list a bit more interessting each customer in the list should have a different preselected skill:<\/p>\n<pre>\r\n@RequestMapping(value = \"\/test\", method = RequestMethod.GET)\r\npublic String provide(ModelMap modelMap){\r\n\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\t\r\n\tList&lt;String&gt; l = new ArrayList&lt;String&gt;();\r\n\tl.add(\"Hibernate\");\r\n\tl.add(\"Spring\");\r\n\tl.add(\"Apache Wicket\");\r\n\tl.add(\"Struts\");\r\n\t\r\n\tRandom rand = new Random(System.nanoTime());\r\n\t\r\n\tList&lt;Customer&gt; list = new ArrayList&lt;Customer&gt;();\r\n\tfor (int i=0;i&lt;4;i++){\r\n\t\tCustomer cust = new Customer();\r\n\t\tString skill = l.get(rand.nextInt(4));\r\n\t\tcust.setJavaSkills(skill);\r\n\t\tlist.add(cust);\r\n\t}\r\n\tCustomerList cl = new CustomerList();\r\n\tcl.setList(list);\r\n\tmodelMap.addAttribute(\"customerForm\", cl);\t\r\n\t\r\n\treturn \"config_test\";\r\n}\r\n<\/pre>\n<h3>Applying changes to the JSP<\/h3>\n<p>In the previous example we had a table with two rows; one for the county selection and one for the skill. Since our model is now a list of Customers (4 to be precise) we have to add these two rows four times. We do that by iterating through the list. The essential point is that we define a variable to store the current status. We will need this status to access the correct element in each iteration process.<br \/>\nIn the select elements the path is augmented: it is now the list at the index and then the appropriate field:<\/p>\n<pre>\r\n&lt;form:form method=\"POST\" commandName=\"customerForm\" action=\"${link_url_home_config_test_save}\"&gt;\r\n\t&lt;table&gt;\r\n\t \t&lt;c:forEach items=\"${customerForm.list }\" var=\"customer\" varStatus=\"x\"&gt;\r\n\t\t&lt;tr&gt;\r\n\t\t\t&lt;td&gt;Country : &lt;\/td&gt;\r\n\t\t\t&lt;td&gt;\r\n\t\t\t\t&lt;form:select path=\"list[${x.index}].country\"&gt;\r\n\t\t\t\t\t&lt;form:option value=\"NONE\" label=\"--- Select ---\"\/&gt;\r\n\t\t\t\t\t&lt;form:options items=\"${countryList}\" \/&gt;\r\n\t\t\t\t&lt;\/form:select&gt;\r\n\t\t\t&lt;\/td&gt;\r\n\t\t&lt;\/tr&gt;\t\t \r\n\t\t&lt;tr&gt;\r\n\t\t\t&lt;td&gt;Java Skills : &lt;\/td&gt;\r\n\t\t\t&lt;td&gt;\r\n\t\t\t\t&lt;form:select path=\"list[${x.index}].javaSkills\" items=\"${javaSkillsList}\" multiple=\"true\" \/&gt;\r\n\t\t\t&lt;\/td&gt;\r\n\t\t\t&lt;\/tr&gt;\t\t\t \r\n\t \t&lt;\/c:forEach&gt;\r\n\t\t&lt;tr&gt;\r\n\t\t\t&lt;td colspan=\"3\"&gt;&lt;input type=\"submit\" \/&gt;&lt;\/td&gt;\r\n\t\t&lt;\/tr&gt;\r\n\t&lt;\/table&gt;\r\n&lt;\/form:form&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my previous article I have shown how to create a simple form with two select boxes using spring form from MVC. When we have an application with may forms we want to be able to use a general model for backing the data. We also might want to have certain elements multiple times, e.g. &hellip; <a href=\"http:\/\/sahits.ch\/blog\/blog\/2010\/11\/26\/generifing-mvc-spring-select-lists\/\" class=\"more-link\"><span class=\"screen-reader-text\">\u201eGenerifing MVC spring select lists\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-945","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\/945","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=945"}],"version-history":[{"count":10,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/945\/revisions"}],"predecessor-version":[{"id":966,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/945\/revisions\/966"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=945"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}