Home > FAQs > Cookbook > Using Checkboxes

The W3C specification does not provide a way to submit a "cleared" or "false" checkbox. If the control is clear, the browser will not submit any value for that control. The application is expected to notice that the checkbox was not submitted, and proceed accordingly.

The framework automatically tracks the checkboxes used by a form (so you don't have to). If a checkbox is missing, a default value for the checkbox (usually false) is injected. The checkbox control can then turn on-and-off values as needed,

Using Checkboxes to Set a Collection

Our user has a number of priviliges that are stored as a Set of strings. To use checkboxes for these, we have HTML that looks like:

<input type="checkbox" name="user.priv" value="boss"/>
<input type="checkbox" name="user.priv" value="admin"/>
<input type="checkbox" name="user.priv" value="manager"/>

Say a user checks the first 2; the browser will send the query string: user.priv=boss&user.priv=admin.

OGNL will end up calling

action.getUser().setPriv(String[] {"boss", "admin"})

You can write this method like:

    Set m_privileges = new HashSet();

    public void setPriv(String[] privs) {
        for (int i = 0; i < privs.length; i++) {
            m_privileges.add(privs[i]);
        }
    }

Full Detailed example:

This example uses a kind-of model-driven action (see Model Driven Interceptor). The action returns a single getter for the User object whose values are populated.