Search code examples
formsspringspring-mvccheckboxjspx

form:checkboxes unchecked by default


in jspx I have :

 <form:form commandName="wordingPractice" autocomplete="off">
    <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
    <form:checkboxes items="${wordingPractice.answers}" path="answers" 
          delimiter="&lt;br/&gt;" checked=""/>
</form:form>

And I see the checked checkboxes when I open the view.

But how to make this checkboxes unchecked?


Solution

  • The problem is in path="answers". answers is the list of items to be displayed as checkboxes as its mentioned in items. Your path is also answers which is supposed to hold selected answers. So path value matches with items and so all check boxes are selected. To overcome this, create array selectedAnswers to hold comma separated values of selected ones in wordingPractice:

        private String [] selectedAnswers;
        //setters and getters
    

    Change jsp as:

        <form:form commandName="wordingPractice" autocomplete="off">
            <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
            <form:checkboxes items="${wordingPractice.answers}" path="selectedAnswers"/>
        </form:form>