{"id":955,"date":"2010-11-26T16:16:49","date_gmt":"2010-11-26T15:16:49","guid":{"rendered":"http:\/\/sahits.ch\/blog\/?p=955"},"modified":"2010-11-30T13:07:46","modified_gmt":"2010-11-30T12:07:46","slug":"generic-model-for-form-select","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2010\/11\/26\/generic-model-for-form-select\/","title":{"rendered":"Generic model for form select"},"content":{"rendered":"<p>A further improvement would be to have a model for a dropdown list that is not domain specific. This is especially useful when the whole form contains only dropdown lists. So this builds upon <a href=\"http:\/\/sahits.ch\/blog\/?p=945\">this article<\/a>.<!--more--><br \/>\nAn element for a dropdown list consists of a label, the selected value and a list of the options:<\/p>\n<pre>\r\npublic static class SelectElement{\r\n\tprivate String label;\r\n\tprivate String selected;\r\n\tprivate Map&lt;String, String&gt; options;\r\n\tpublic String getLabel() {\r\n\t\treturn label;\r\n\t}\r\n\tpublic void setLabel(String label) {\r\n\t\tthis.label = label;\r\n\t}\r\n\tpublic String getSelected() {\r\n\t\treturn selected;\r\n\t}\r\n\tpublic void setSelected(String selected) {\r\n\t\tthis.selected = selected;\r\n\t}\r\n\t\r\n\tpublic Map&lt;String, String&gt; getOptions() {\r\n\t\treturn options;\r\n\t}\r\n\tpublic void setOptions(Map&lt;String, String&gt; options) {\r\n\t\tthis.options = options;\r\n\t}\r\n}\r\n<\/pre>\n<p>The actual model for this is a List of these objects as shown in the <a href=\"http:\/\/sahits.ch\/blog\/?p=945\">last article<\/a>. The controller method then looks like this:<\/p>\n<pre>\r\n\t@RequestMapping(value = \"\/test\", method = RequestMethod.GET)\r\n\tpublic String provide(ModelMap modelMap){\r\n\t\tRandom rand = new Random(System.nanoTime());\r\n\t\tMap&lt;String, String&gt; country = new LinkedHashMap&lt;String, String&gt;();\r\n\t\tcountry.put(\"CHINA\", \"China\");\r\n\t\tcountry.put(\"SG\", \"Singapore\");\r\n\t\tcountry.put(\"MY\", \"Malaysia\");\r\n\t\tcountry.put(\"US\", \"United Stated\");\r\n\r\n\t\tMap&lt;String, String&gt; javaSkill = new LinkedHashMap&lt;String, String&gt;();\r\n\t\tjavaSkill.put(\"Hibernate\", \"Hibernate\");\r\n\t\tjavaSkill.put(\"Spring\", \"Spring\");\r\n\t\tjavaSkill.put(\"Apache Wicket\", \"Apache Wicket\");\r\n\t\tjavaSkill.put(\"Struts\", \"Struts\");\r\n\t\t\r\n\r\n\t\tList&lt;String&gt; l0 = new ArrayList&lt;String&gt;();\r\n\t\tl0.add(\"China\");\r\n\t\tl0.add(\"Singapore\");\r\n\t\tl0.add(\"Malaysia\");\r\n\t\tl0.add(\"United Stated\");\r\n\r\n\t\tList&lt;String&gt; l2 = new ArrayList&lt;String&gt;();\r\n\t\tl2.add(\"Hibernate\");\r\n\t\tl2.add(\"Spring\");\r\n\t\tl2.add(\"Apache Wicket\");\r\n\t\tl2.add(\"Struts\");\r\n\r\n\t\t\r\n\t\tList&lt;SelectElement&gt; l = new ArrayList&lt;SelectElement&gt;();\r\n\t\tfor (int i=0;i&lt;4;i++){\r\n\t\t\tSelectElement e = new SelectElement();\r\n\t\t\tString skill = l0.get(rand.nextInt(4));\r\n\t\t\tlogger.debug(\"Country: \"+skill);\r\n\t\t\te.setLabel(\"Country : \");\r\n\t\t\te.setSelected(skill);\r\n\t\t\te.setOptions(country);\r\n\t\t\tl.add(e);\r\n\t\t\te = new SelectElement();\r\n\t\t\tskill = l2.get(rand.nextInt(4));\r\n\t\t\te.setLabel(\"Java Skill : \");\r\n\t\t\te.setSelected(skill);\r\n\t\t\te.setOptions(javaSkill);\r\n\t\t\tl.add(e);\r\n\t\t}\r\n\t\tSelectElementList l1 = new SelectElementList();\r\n\t\tl1.setList(l);\r\n\t\tmodelMap.put(\"customerForm\", l1);\r\n\t\t\r\n\t\treturn \"config_test\";\r\n\t}\r\n<\/pre>\n<p>This makes the JSP pretty simple:<\/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\t&lt;tr&gt;\r\n\t \t\t\t&lt;td&gt;&lt;c:out value=\"${customer.label }\"\/&gt;&lt;\/td&gt;\r\n\t \t\t\t&lt;td&gt;\r\n\t \t\t\t\t&lt;form:select path=\"list[${x.index}].selected\" items=\"${customer.options }\"&gt;\r\n\t \t\t\t\t&lt;\/form:select&gt;\r\n\t \t\t\t&lt;\/td&gt;\r\n\t \t\t&lt;\/tr&gt;\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>A further improvement would be to have a model for a dropdown list that is not domain specific. This is especially useful when the whole form contains only dropdown lists. So this builds upon this article.<\/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-955","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\/955","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=955"}],"version-history":[{"count":4,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"predecessor-version":[{"id":965,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/955\/revisions\/965"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}