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 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.

JSF controls can be bound to a value in a map using the syntax #{some.map['someKey']} where someKey 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.

  1. Create a new backing bean called FormBean with the following code :
    public class FormBean {
    
    	private List<String> items;
    	
    	private Map<String,Boolean> checkMap = new HashMap<String,Boolean>();
    
    	public FormBean() {
    		items = new ArrayList<String>();
    		items.add("Open");
    		items.add("Closed");
    		items.add("Pending");
    		items.add("Suspended");
    		items.add("In Progress");
    		items.add("Rejected");
    		
    		//fill the check map with the items defaulted to unchecked
    		for (String item : items) {
    			checkMap.put(item,Boolean.FALSE);
    		}
    	}
    	
    	public List<String> getItems() {
    		return items;
    	}
    
    	public Map<String, Boolean> getCheckMap() {
    		return checkMap;
    	}
    
    }
    

    This creates our list of items and populates it with a set of values. The checkMap will be used to track which items are selected and which are not. The map contains a set of String,Boolean pairs.

  2. Add one more method to return a string telling us which items are selected
    	public String getSelected() {
    		String result = "";
    		for (Entry<String,Boolean> entry : checkMap.entrySet()) {
    			if (entry.getValue()) {
    				result = result + ", "+entry.getKey(); 
    			}			
    		}
    		return result.length() == 0 ? "" : result.sub string(2);
    	}
    

    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.

  3. Our backing bean is complete, now we will create our view
    <h:form>
    	<h:outputText value="Currently Selected : #{formBean.selected}" />
    
    	<ui:repeat var="item" value="#{formBean.items}">
    		<h:outputText value="#{item}" />
    		<h:selectBooleanCheckbox value="#{formBean.checkMap[item]}"/>	
    	</ui:repeat>
    
    	<h:commandButton value="Update" action="refresh" />
    </h:form>
    

    We iterate through the list of items and for each one, display the caption (the item itself) and a checkbox bound to the Boolean checkMap entry for that item.

    JSF CheckBox Map Screenshot

Unfortunately, we can’t iterate through the keySet because JSF doesn’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.

If you want to make the list update when you click an item, just add the f:ajax tag to the checkbox like so :

<h:selectBooleanCheckbox value="#{formBean.checkMap[item]}">
	<f:ajax event="click" execute="@form" render="@form" />
</h:selectBooleanCheckbox>

You can download the working code for this example (JsfCheckMap Demo Source Code) which you can run by unzipping it and in a command line running mvn jetty:run or windows users can just double click run.bat.