rdiazroman
rdiazroman

Reputation: 429

Submit boolean if checkbox is checked or not. JSP

I want to send to the BackEnd if an input checkbox has been checked or not.

<input id="firstRowData" type="checkbox" class="checkbox-big" name="firstRowData" value="this_is_the_value"/>

Is this the correct way? Because I'm having an exception:

org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Boolean'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [2]

I'm reading the parameter in the Backend this way:

final Boolean firstRowData, @RequestParam(value = "firstRowData", required = false)

Upvotes: 1

Views: 3993

Answers (3)

Julien
Julien

Reputation: 2756

The problem is that when the checkbox is not checked, the form submition doesn't even include this input (its name and value) to the parameter list.

You will have to check if the parameter is sent and when not set false to your boolean.

Or you could have an hidden input that contains the real value and then toggle this value ("true"/"false") with a javascript event binded to a dummy checkbox with no name that will not be considered in your form submition.

Upvotes: 1

meda
meda

Reputation: 45490

When you are posting a form with check box you might get a value.

To determine if the checkbox was checked or not you need to verify on the server side if a value was posted, if the value is not posted you can assume the check box was not checked.

Upvotes: 1

Tony
Tony

Reputation: 482

The value field should contains a boolean value.

you need to replace "this_is_the_value" by true or false

Upvotes: 1

Related Questions