{"id":1519,"date":"2010-10-27T08:17:06","date_gmt":"2010-10-27T13:17:06","guid":{"rendered":"http:\/\/www.andygibson.net\/blog\/?p=1519"},"modified":"2010-11-12T00:03:21","modified_gmt":"2010-11-12T05:03:21","slug":"binding-dynamic-multi-select-checkboxes-with-jsf","status":"publish","type":"post","link":"https:\/\/www.andygibson.net\/old-blog\/tutorial\/binding-dynamic-multi-select-checkboxes-with-jsf\/","title":{"rendered":"Binding Dynamic Multi-select Checkboxes with JSF"},"content":{"rendered":"<p>This tutorial looks at how JSF makes it easy to create a dynamic list of checkboxes that can be used to select values on the server.<\/p>\n<p>Given a list of strings, we want to display the list of values, each with a check box and be able to select or deselect that item from inclusion in the selected set.  We want to do it without having to manually add each checkbox to the page and bind it to a property on the backing bean.<br \/>\n<!--more--><br \/>\nJSF controls can be bound to a value in a map using the syntax <code>#{some.map['someKey']}<\/code> where <code>someKey<\/code> can also be an EL expression. This means we can bind a checkbox to a Boolean value in the map. If we have an iterable list of of key values, we can bind multiple checkboxes to different values in a map.<\/p>\n<ol>\n<li>Create a new backing bean called <code>FormBean<\/code> with the following code :\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class FormBean {\r\n\r\n\tprivate List&lt;String&gt; items;\r\n\t\r\n\tprivate Map&lt;String,Boolean&gt; checkMap = new HashMap&lt;String,Boolean&gt;();\r\n\r\n\tpublic FormBean() {\r\n\t\titems = new ArrayList&lt;String&gt;();\r\n\t\titems.add(&quot;Open&quot;);\r\n\t\titems.add(&quot;Closed&quot;);\r\n\t\titems.add(&quot;Pending&quot;);\r\n\t\titems.add(&quot;Suspended&quot;);\r\n\t\titems.add(&quot;In Progress&quot;);\r\n\t\titems.add(&quot;Rejected&quot;);\r\n\t\t\r\n\t\t\/\/fill the check map with the items defaulted to unchecked\r\n\t\tfor (String item : items) {\r\n\t\t\tcheckMap.put(item,Boolean.FALSE);\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic List&lt;String&gt; getItems() {\r\n\t\treturn items;\r\n\t}\r\n\r\n\tpublic Map&lt;String, Boolean&gt; getCheckMap() {\r\n\t\treturn checkMap;\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>This creates our list of items and populates it with a set of values. The <code>checkMap<\/code> will be used to track which items are selected and which are not.  The map contains a set of String,Boolean pairs.\n<\/li>\n<li>Add one more method to return a string telling us which items are selected\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\tpublic String getSelected() {\r\n\t\tString result = &quot;&quot;;\r\n\t\tfor (Entry&lt;String,Boolean&gt; entry : checkMap.entrySet()) {\r\n\t\t\tif (entry.getValue()) {\r\n\t\t\t\tresult = result + &quot;, &quot;+entry.getKey(); \r\n\t\t\t}\t\t\t\r\n\t\t}\r\n\t\treturn result.length() == 0 ? &quot;&quot; : result.sub string(2);\r\n\t}\r\n<\/pre>\n<p>To get the list of selected items, we iterate through the set of entries and if the value in the map is true, then it is selected and added to the result string.\n<\/li>\n<li>Our backing bean is complete, now we will create our view\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;h:form&gt;\r\n\t&lt;h:outputText value=&quot;Currently Selected : #{formBean.selected}&quot; \/&gt;\r\n\r\n\t&lt;ui:repeat var=&quot;item&quot; value=&quot;#{formBean.items}&quot;&gt;\r\n\t\t&lt;h:outputText value=&quot;#{item}&quot; \/&gt;\r\n\t\t&lt;h:selectBooleanCheckbox value=&quot;#{formBean.checkMap&#x5B;item]}&quot;\/&gt;\t\r\n\t&lt;\/ui:repeat&gt;\r\n\r\n\t&lt;h:commandButton value=&quot;Update&quot; action=&quot;refresh&quot; \/&gt;\r\n&lt;\/h:form&gt;\r\n<\/pre>\n<p>We iterate through the list of items and for each one, display the caption (the item itself) and a checkbox bound to the Boolean <code>checkMap<\/code> entry for that item.<\/p>\n<div class=\"contentBox alignCenter\">\n<a href=\"http:\/\/localhost\/wordpress\/wp-content\/uploads\/2010\/09\/JSFCheckMapScreen.png\"><img loading=\"lazy\" src=\"http:\/\/localhost\/wordpress\/wp-content\/uploads\/2010\/09\/JSFCheckMapScreen-300x257.png\" alt=\"JSF CheckBox Map Screenshot\" title=\"JSF CheckBox Map Screenshot\" width=\"300\" height=\"257\"  \/><\/a>\n<\/div>\n<\/ol>\n<p>Unfortunately, we can&#8217;t iterate through the <code>keySet<\/code> because JSF doesn&#8217;t provide for iteration over Sets (next version JSF Expert Group!) so we have to use a separate list as our key. However, this is a nice way of providing a dynamic set of checkbox controls that are bound to dynamic values without having to use any tricks to keep track of control bindings.<\/p>\n<p>If you want to make the list update when you click an item, just add the <code>f:ajax<\/code> tag to the checkbox like so : <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;h:selectBooleanCheckbox value=&quot;#{formBean.checkMap&#x5B;item]}&quot;&gt;\r\n\t&lt;f:ajax event=&quot;click&quot; execute=&quot;@form&quot; render=&quot;@form&quot; \/&gt;\r\n&lt;\/h:selectBooleanCheckbox&gt;\r\n<\/pre>\n<p>You can download the working code for this example (<a href='http:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2010\/09\/JsfCheckMap.zip'>JsfCheckMap Demo Source Code<\/a>) which you can run by unzipping it and in a command line running <code>mvn jetty:run<\/code> or windows users can just double click <code>run.bat<\/code>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial looks at how JSF makes it easy to create a dynamic list of checkboxes that can be used to select values on the server. Given a list of strings, we want to display the list of values, each with a check box and be able to select or deselect that item from inclusion [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[64],"tags":[],"_links":{"self":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts\/1519"}],"collection":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/comments?post=1519"}],"version-history":[{"count":27,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts\/1519\/revisions"}],"predecessor-version":[{"id":1640,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts\/1519\/revisions\/1640"}],"wp:attachment":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/media?parent=1519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/categories?post=1519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/tags?post=1519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}