It's hard to imagine a situation when a modern web-application doesn't have checkboxes in a form. In one of the previous posts I wrote about form handling in Spring MVC, as a continuation of this series I'm going to write about Spring MVC forms, and in particular about checkbox processing. This post will be about a standard usage of tag.
So you can see that these three points are simple enough, but they need some attention from developer's side. Below I will describe how to realize each of these cases, you can download the source code in the end of the article.
Notice that usage of Spring form tag implies import of the appropriate tag library. The checkbox tag contains the attribute path which corresponds to the property of the domain model.
Notice that a new attribute appears in the checkbox tag. The value attribute represents the string property from the domain model.
The values from the checkbox tags will be passed to the string array during the processing.
When a checkbox in an HTML page is not checked, its value will not be sent to the server as part of the HTTP request parameters once the form is submitted, so we need a workaround for this quirk in HTML in order for Spring form data binding to work. The checkbox tag follows the existing Spring convention of including a hidden parameter prefixed by an underscore ("_") for each checkbox. By doing this, you are effectively telling Spring that " the checkbox was visible in the form and I want my object to which the form data will be bound to reflect the state of the checkbox no matter what ".
This article gives you a high level overview of the checkbox tag usage, but this is just a base. In the nearest posts I will show you more interesting situations of the checkbox usage in Spring MVC.
You can download the source code of the entire application clicking on the link.