Spring MVC Form Checkbox

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:

 Dancing <form:checkbox path = "hobbies" value = "Dancing" />
 Singing <form:checkbox path = "hobbies" value = "Singing" /> 

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:

<form:checkboxes path =
"hobbies" items = "${student.list}" />  

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.


 <h2> Spring MVC Web application </h2>
 <a href = "student_form"> Student Form </a>
  •  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.


 import java.util.LinkedHashMap;
 public class Student {
             private String fname;
             private String lname;
             private String country;
             private LinkedHashMap<String, String> countryoptions;
             private String planguage;
             private String[] hobbies;
             public Student() {
               countryoptions = new LinkedHashMap<String, String>();
                         countryoptions.put("NEP", "Nepal");
                         countryoptions.put("BHU", "Bhutan");
                         countryoptions.put("UK", "United Kingdom");
             public String getCountry() {
                         return country;
             public LinkedHashMap<String, String> getCountryoptions() {
                         return countryoptions;
             public void setCountry(String country) {
                         this.country = country;
             public String getFname() {
                         return fname;
             public void setFname(String fname) {
                         this.fname = fname;
             public String getLname() {
                         return lname;
             public void setLname(String lname) {
                         this.lname = lname;
             public String getPlanguage() {
                         return planguage;
             public void setPlanguage(String planguage) {
                         this.planguage = planguage;
             public String[] getHobbies() {
                         return hobbies;
             public void setHobbies(String[] hobbies) {
                         this.hobbies = hobbies;
  • Create the Controller class

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


 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 public class StudentController {
             public String showStudentForm( Model m) {
                         Student student = new Student();
                         m.addAttribute("student", student);
                         return "studentform" ;
             public String showStudentData(@ModelAttribute("student") Student student) {
                         return "student-data" ;
  • 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.


 <?xml version = "1.0" encoding = "UTF-8"?>
 <web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
             xmlns = "http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id = "WebApp_ID" version = "3.1">
             <absolute-ordering />
             <!-- Spring MVC Configs -->
             <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
             <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
  • 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).


 <?xml version = "1.0" encoding = "UTF-8"?>
 <beans xmlns = "http://www.springframework.org/schema/beans"
             xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:context = "http://www.springframework.org/schema/context"
             xmlns:mvc = "http://www.springframework.org/schema/mvc"
             xsi:schemaLocation = "  http://www.springframework.org/schema/beans
             <!-- Step 3: Add support for component scanning -->
             <context:component-scan base-package = "com.app.SpringMVCFormTag"  />
             <!-- Step 4: Add support for conversion, formatting and validation support -->
             <!-- Step 5: Define Spring MVC view resolver -->
                         <property name = "prefix" value = "/WEB-INF/view/" />
                         <property name = "suffix" value = ".jsp" />
  • Create all the view pages

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


 <%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %>  
 <title>Student Registration Form
 <form:form action = "studentregis" modelAttribute = "student" >
 FIRST NAME: <form:input path = "fname" />
 LAST NAME: <form:input path = "lname" />
 COUNTRY: <form:select path="country">
    <form:options items = "${student.countryoptions}"></form:options>
 Java <form:radiobutton path = "planguage" value = "Java" />
 Python <form:radiobutton path = "planguage" value = "Python" />
 C++ <form:radiobutton path = "planguage" value = "C++" />
 PHP <form:radiobutton path = "planguage" value = "PHP" />
 Dancing <form:checkbox path = "hobbies" value = "Dancing" />
 Singing <form:checkbox path = "hobbies" value = "Singing" />
 Travelling <form:checkbox path = "hobbies" value = "Travelling"/>
 Reading <form:checkbox path = "hobbies" value = "Reading" />
 <input type = "submit" value = "Submit"/>


 <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
 <!DOCTYPE html>
 <title>Student Registration Data</title>
 The student name is ${student.fname} ${student.lname}
 Country: ${student.country}
 Programming Language: ${student.planguage}
    <c:forEach var = "temp" items ="${student.hobbies}">
        <li> ${temp} </li>


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