Spring MVC provides several form tags used to develop web applications (Usually JSP pages). One of them is a checkbox tag. The checkbox tag offers us to choose multiple options at the same time.

Below is given a code snippet which shows how to use the <form:checkbox> tag:

The <form:checkbox> tag is used to create an HTML checkbox field in which the values are hard-coded inside the JSP pages, whereas the <form:checkboxes> tag is used to create multiple checkboxes in which the checkbox values are generated at run time. The <fom:checkboxes> tag is only used when we don’t want to list down the elements in the JSP page.

Below is given a code snippet, which shows how to use the <form:checkboxes> tag:

Example of MVC Form Checkbox

Here, we are going to create an example of <form:checkbox>.


Following are the steps to create an example of <form:checkbox> tag:

  • Create a request page

In this step, we are going to create a request page named index.jsp.

index.jsp

  •  Create the Model class which stores data

In this step, we are going to create a Model class named Student.java, which stores the checkbox values.

Student.java

  • Create the Controller class

In this step, we are going to create a Controller named StudentController.java.

StudentController.java

  • Add the entry of Controller in the web.xml

In this step, we are going to add the entry of the Controller inside the web.xml.

web.xml

  • Define the Model class in another XML file

In this step, we are going to define the bean in another XML file (spring-servlet.xml).

spring-servlet.xml

  • Create all the view pages

In this step, we are going to create the other view pages (JSP pages).

studentform.jsp

student-data.jsp

Output

Spring MVC Form Checkbox
Spring MVC Form Checkbox 1
Spring MVC Form Checkbox 2
Spring MVC Form Checkbox 3

Pin It on Pinterest

Share This