Yasitha Bandara
Yasitha Bandara

Reputation: 2385

how to receive html check box value in spring mvc controller

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.

<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");
  }
}

Upvotes: 12

Views: 27875

Answers (4)

Paolo
Paolo

Reputation: 1691

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 :)

Upvotes: 17

iagorubio
iagorubio

Reputation: 121

I think the proper answers are Paolo's and Phoste's ones.

ITOH, if you have many checkboxes/params/lasrge forms you can use a Map<T,T> RequestParam, so if the property does not exists - Map.get() return null - the checkbox is not checked.

    @RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
    public void editCustomer(@RequestParam Map<String,String> params) 
    {
        String checkboxValue = map.get("checkboxName");
        if(checkboxValue != null)
        {
            System.out.println("checkbox is checked");
            return;
        }
        System.out.println("checkbox is not checked");
    }

Upvotes: 1

Phoste
Phoste

Reputation: 1209

I had the same problem and I finally found a simple solution.

Just add a default value to false and it will work out perfectly.

Html code :

<form action="path" method="post">
    <input type="checkbox" name="checkbox"/>
</form>

Java code :

@RequestMapping(
    path = "/path",
    method = RequestMethod.POST
)
public String addDevice(@RequestParam(defaultValue = "false") boolean checkbox) {
    if (checkbox) {
        // do something if checkbox is checked
    }

    return "view";
}

Upvotes: 12

Yasitha Bandara
Yasitha Bandara

Reputation: 2385

I had to add hidden input with same name of the checkbox. Value must be "checked".Then I can check the length of the string array within controller or in my service class.

<form action="editCustomer" method="post">
  <input type="hidden" name="checkboxName" value="checked">
  <input type="checkbox" name="checkboxName"/>
</form>

@RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
	public void editCustomer(@RequestParam("checkboxName")String[] checkboxValue) 
	{
		if(checkboxValue.length==2)
        	{
          	  System.out.println("checkbox is checked");
        	}
		else
        	{
          	  System.out.println("checkbox is not checked");
        	}
	}

Upvotes: 2

Related Questions