How to get check box value in controller?

 Here is the simple way to pass the check box value from a .jsp page to a Java controller.

Spring MVC

Step 1: Model Class - Initialize the checkbox variable in the model class.

Model Class


Step 2: JSP page - Take a checkbox item and provide the name attribute as declare a variable in a Model class. Like - checkBoxItem

Jsp Page

Here have a c:if, which checking the value of checkBoxItem is Y or not with the page loading. If the value is Y then it makes the check box a checked.

Code -

<input  type="checkbox" name="checkBoxItem" value="Y" <c:if test="${model.checkBoxItem=='Y'}">checked="checked"</c:if> />


Step 3: Controller Class - Now we can get the value using @ModelAttribute

Controller

Finally Output :

Output


Post a Comment

0 Comments