DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Binding Dynamic Multi-select Checkboxes with JSF

Andy Gibson user avatar by
Andy Gibson
·
Oct. 28, 10 · Interview
Like (0)
Save
Tweet
Share
44.65K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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.

From http://www.andygibson.net/blog/tutorial/binding-dynamic-multi-select-checkboxes-with-jsf/

Binding (linguistics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Solving the Kubernetes Security Puzzle
  • Secure APIs: Best Practices and Measures
  • How To Build a Spring Boot GraalVM Image

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: