Issue
The problem is if the check box is not checked request can not find the correct mapping function in springMVC controller.Because it seems like it only send true values if it is checked,but it does not send false value if it not checked.
data-hide="false" data-console="true">
<form action="editCustomer" method="post">
<input type="checkbox" name="checkboxName"/>
</form>
@RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
public void editCustomer(@RequestParam("checkboxName")String[] checkboxValue)
{
if(checkboxValue[0])
{
System.out.println("checkbox is checked");
}
else
{
System.out.println("checkbox is not checked");
}
}
Solution
I solved a similar problem specifying required = false
in @RequestMapping
.
In this way the parameter checkboxValue
will be set to null
if the checkbox is not checked in the form or to "on"
if checked.
@RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
public void editCustomer(@RequestParam(value = "checkboxName", required = false) String checkboxValue)
{
if(checkboxValue != null)
{
System.out.println("checkbox is checked");
}
else
{
System.out.println("checkbox is not checked");
}
}
Hope this could help somebody :)
Answered By - Paolo
Answer Checked By - Mary Flores (JavaFixing Volunteer)