Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring - MVC Form Checkbox
Next article icon

Spring - MVC Form Checkbox

Last Updated : 24 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn about the Spring MVC Checkbox and Checkboxes tags. We will create a basic Spring MVC project in the Spring tool suite(STS) to create checkboxes using form:checkbox and form:checkboxes tags.

'spring-form.tld' tag library

In Spring Framework, we can use Java Server Pages (JSPs) as a view component. To implement views using JSP, Spring Framework provides a form tag library namely spring-form.tld with some tags for evaluating errors, setting themes, formatting the fields for inputting and outputting internationalized messages.

'checkbox' tag: The 'checkbox' is one of the tag provided by the spring-form.tld library. It renders an HTML 'input' tag with type 'checkbox'.

HTML
<form:checkbox path=" " value=" " /> 

'checkboxes' tag: The 'checkbox' is one of the tag provided by the spring-form.tld library. It renders multiple HTML 'input' tags with type 'checkbox'.

HTML
<form:checkboxes path=" " items=" " /> 

Spring MVC Application

We will be creating the below Spring MVC application,

Spring MVC - Checkbox Example

Steps to Create an Application

  1. Create a Spring MVC project in Spring Tool Suite.
  2. In STS, while creating the project based on the developer selection, it will download all the required maven dependencies, *.jar, lib files and it will provide an embedded server.
  3. Below is the final project structure of the Spring MVC project after creating *.java and *.jsp files also.
Project Structure

Implementation: Files to be created are as follows:

  1. Home.java - Bean class - To define the properties and getter/setter methods of the properties.
  2. HomeController.java - Controller class - To process the user request and generate the output.
  3. home.jsp - Jsp file to interact with the user for the input.
  4. summary.jsp - Jsp file to display the output after processing to the user.

A. File: home.jsp

HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Home page</title> </head> <body>      <h1>Welcome to GeeksforGeeks!</h1>      <form:form action="submit" method="post" modelAttribute="home">         <table>             <tr>                 <td><form:label path="name">Enter your name: </form:label></td>                 <td><form:input path="name" /></td>             </tr>             <tr>                 <td><form:label path="duration">Select duration of course: </form:label></td>                 <td><form:checkbox path="duration" value="3 months" label="3 Months"/>                      <form:checkbox path="duration" value="6 months" label="6 Months" disabled="true"/></td>             </tr>             <tr>                 <td><form:label path="framework">Java Frameworks: </form:label></td>                 <td><form:checkboxes path="framework" items="${javaFramworks}" delimiter="|"/></td>             </tr>             <tr>                 <td><form:button>Submit</form:button></td>             </tr>         </table>     </form:form>  </body> </html> 

Output explanation: 

This is the home page displayed to the user when the application starts. To use Spring form tags, first, we need to include the taglib URI - "http://www.springframework.org/tags/form" in the Jsp page. In the form tag - action, method attributes are used to map the controller method that has to be executed when the page submits. The checkbox tag has many attributes like path, cssStyle, dir, value, etc. We can include any number of attributes based on our requirements.

In this example, we have included the below attributes,

  1. path - To specify the path to the property that needs to be bound with the data. Here, the data-bind to duration property in checkbox and framework property in checkboxes tags.
  2. value - To specify the value to the particular checkbox.
  3. label - To display the name of the value for the checkbox.
  4. disabled - It is a boolean attribute to make the checkbox disabled when the value is true. By default, the value will be false.
  5. items - To display the checkboxes names from the list.
  6. delimiter - To specify a delimiter to use between each 'input' tag with type 'checkbox'. Here we are using '|' symbol as a delimiter.

B. Home.java

Java
// Java Program to Illustrate Home Class   package com.geek.app;  // Class public class Home {      // Class data members     public String name;     public String duration;     public String framework;      // Getter     public String getName() { return name; }      // Setter     public void setName(String name) { this.name = name; }      // Getter     public String getDuration() { return duration; }      // Setter     public void setDuration(String duration)     {         this.duration = duration;     }      // Getter     public String getFramework() { return framework; }      // Setter     public void setFramework(String framework)     {         this.framework = framework;     } } 
  • This is the java bean class to define the required parameters.
  • Here, we are defining name, duration, and framework as parameters and their getter/setter methods to get/set the values to the properties.

C. HomeController.java

Java
// Java Program to Illustrate HomeController Class  package com.geek.app;  // importing required classes import java.util.Arrays; import java.util.List; 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.RequestMethod;  // Annotation @Controller  // Class public class HomeController {      // Annotation     @RequestMapping(value = "/")      public String viewHome(Model model)     {          // Creating an instance of Home class         // inside this method         Home home = new Home();         model.addAttribute("home", home);          return "home";     }      // Annotation     @ModelAttribute("javaFramworks")      // Method     public List<String> javaFramworksList()     {         List<String> framework = Arrays.asList(             "Apache Struts", "Spring", "Hibernate",             "Grails", "Java Server Faces");          return framework;     }      // Annotation     @RequestMapping(value = "/submit",                     method = RequestMethod.POST)      // Method     public String     submit(@ModelAttribute("home") Home home)     {          return "summary";     } } 


Output Explanation: This is the controller class where it executes the methods based on the mapping of the request URLs. Here, @Controller, conveys to the container that this class is the spring controller class. To use this annotation we need to import org.springframework.stereotype.Controller package. The annotation, @RequestMapping, maps the request URL's to the specified method based on the value provided. o use this annotation, we need to import org.springframework.web.bind.annotation.RequestMapping package. The annotation @ModelAttribute, used to bind a method parameter or method return value to the named model attribute. We need to import org.springframework.web.bind.annotation.ModelAttribute package. The RequestMethod specifies the type of the request whether it is a get request or post request.

D. File: summary.jsp

HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Summary page</title> </head> <body>      <h3>Hello ${home.name}!!</h3>      <span>Course Duration: </span>     <span>${home.duration}</span>     <br>     <span>Frameworks Selected: </span>     <span>${home.framework}</span>  </body> </html> 
  • This is the output Jsp page to display the user entered values in the browser after the processing of the request.

Execution

  • After creating all the required .java and .jsp files, run the project on the server.
  • Right on the Project, Run as -> Run on Server.
  • Select the server in the localhost to run the application.
  • Open the URL: http://localhost:8080/app/ in the browser to get the below screen.
Home Page - home.jsp

As we specified,

  • disabled=true for 6 Months checkbox, it is showing disabled in the screen.
  • delimiter = |, the specified delimiter is displaying between the checkbox names.

Now, select the checkbox values and click on submit button.

Selection of the input
  • Once the page is submitted, we will get the below output with the details we have selected.
Output - summary.jsp

Next Article
Spring - MVC Form Checkbox

Y

yaminitalisetty
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Spring
  • Java-Spring-MVC
Practice Tags :
  • Java

Similar Reads

    Spring MVC Integration with MySQL
    Spring MVC is one of the most popular Java frameworks for building scalable web applications. When combined with MySQL, it provides a robust solution for developing data-driven applications. This article will guide you through integrating Spring MVC with MySQL, covering database setup, project confi
    7 min read
    OpenSource REST API URL and Retrieving Data From it By Using Spring MVC
    In this internet era, a lot of helper services are available in the form of REST API and mostly REST API provides us the details in the form of JSON/XML. We can use them in our applications and render the data as we like. Here is the list of a few opensource REST API URLs: API NameDescriptionURLCoin
    6 min read
    How to Resolve WEB xml is missing and failOnMissingWebXml is set to true in Eclipse/STS?
    Eclipse/STS IDE is generally used to develop Spring applications and what happens is whenever we are creating a simple Maven project and if the web.xml is missing or you have deleted that file then you may encounter this problem inside the pom.xml file corresponding to which do refer to the below im
    2 min read
    Spring MVC Application Without web.xml File
    Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. Here we will be creating and running Your First Spring MVC Application,
    4 min read
    Spring - MVC Listbox
    Spring Web MVC framework to demonstrate how to utilize Listbox in forms. Let's start by setting up an Eclipse IDE and then following the steps to create a Dynamic Form-based Web Application utilizing the Spring Web Framework. The items are listed in the Spring MVC form Listbox. This tag creates a se
    4 min read
    Spring MVC - Multiple Resolver Mapping
    Spring MVC Framework provides the feature of View Resolver through which you can map a view to its respective view and render the model in your browser. The View Resolver eliminates the use of view technologies like Velocity and JSP. There are many view resolvers available in spring-like InternalRes
    4 min read
    How to Extract TV Show Details via REST API and Spring MVC?
    REpresentational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. REST API is a way of accessing web services in a simple and flexible way without having any processing. Spring MVC is a Web MVC Framework for building web applicat
    9 min read
    Spring MVC File Upload
    Spring MVC provides a robust mechanism for handling file uploads in web applications. Using Spring MVC file upload, developers can easily integrate multipart file handling with the CommonsMultipartResolver. This article covers how to upload files in Spring MVC, configure MultipartResolver, and manag
    6 min read
    Spring MVC - Tiles
    Spring provides functionality for integrating with the Apache Tiles framework. With the help of spring tiles support, we can easily manage the layout of the Spring MVC application. Benefits of Spring MVC's Tiles support: The following are some of the primary benefits of Spring MVC Tiles 3 Integratio
    4 min read
    Spring MVC - Text Box
    To begin, make sure you have a working Eclipse IDE installed and follow the steps below to create a Spring Web Framework-based Dynamic Form-based Web Application. Steps to create TextBox in Spring MVC: Create a SpringMVCTextField project in the com.geeksforgeeks package.Under the com.geeksforgeeks p
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences