samedi 28 novembre 2015

SpringMVC Checkbox Always Submitted as TRUE, even if unchecked. Bound to a boolean

My Model object contains a 'boolean' field called expanded, the getters/setters are as follows.

private boolean expanded = false;

    public boolean isExpanded() {
        return expanded;
    }

    public void setExpanded(final boolean expanded) {
        this.expanded = expanded;
    }

JSP:

<form:checkbox path="expanded" />

Even after I uncheck the box on the form and submit, the generated HTML always has this. Note the auto-added Hidden field for FALSE values, it's already there. And note the checkbox is always value=TRUE, checked=CHECKED.

<input id="expanded1" name="expanded" type="checkbox" value="true" checked="checked"/>
<input type="hidden" name="_expanded" value="on"/> 

Thus, the value of "Expanded" is always submitted as TRUE even when I uncheck the box. Is there a problem with binding? There are no binding errors, it finds the property.

I found this thread about unchecked checkboxes not being submitted by the browser, but SpringMVC has already added the Hidden field for me. I shouldn't have to do anything extra: Why spring always resolves <form:checkbox> to true?

Why is the proper FALSE value of the checkbox still not getting transmitted?

I verified in jQuery that immediately after form submission, it's already TRUE. There is no JS modifying the value anywhere.




Aucun commentaire:

Enregistrer un commentaire