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 Listbox
Next article icon

Spring - MVC Listbox

Last Updated : 25 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Spring -MVC Framework

The items are listed in the Spring MVC form Listbox. This tag creates a select element in HTML. It allows you to bind data to the element you've chosen.

Syntax:

<form:select path="name">  

Here are some more tags that may be used to narrow down the selections.

A. Option tag: The HTML option tag is generated by this tag. Each tag has a value that the user can choose from.

<form:option value="abc" label="xyz"/>  

B. Options tag: A list of HTML option tags is generated by this tag. Each tag has a list of components that the user has chosen.

<form:options items="${elementList}" itemValue="abc" itemLabel="xyz"/>  

Spring MVC - Listbox

The project Structure is as follows:

Implementation:

Step 1: Add dependencies to the pom.xml file.

File: pom.xml

XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.geeksforgeeks</groupId>   <artifactId>SpringMVC</artifactId>   <packaging>war</packaging>   <version>0.0.1-SNAPSHOT</version>   <name>SpringMVC Maven Webapp</name>   <url>http://maven.apache.org</url>   <dependencies>     <dependency>       <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>3.8.1</version>       <scope>test</scope>     </dependency>          <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-webmvc</artifactId>     <version>5.1.1.RELEASE</version> </dependency>  <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper --> <dependency>     <groupId>org.apache.tomcat</groupId>     <artifactId>tomcat-jasper</artifactId>     <version>9.0.12</version> </dependency>     <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency>       <groupId>javax.servlet</groupId>       <artifactId>servlet-api</artifactId>       <version>3.0-alpha-1</version>   </dependency>  <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency>     <groupId>javax.servlet</groupId>     <artifactId>jstl</artifactId>     <version>1.2</version> </dependency>    </dependencies>   <build>     <finalName>SpringMVC</finalName>   </build> </project> 

Step 2: Create the bean class

File: Reservation.java

Java
// Java Program to Illustrate Reservation Class  package com.geeksforgeeks;  // Class public class Reservation {     // Class data members     private String firstName;     private String lastName;     private String Gender;     private String[] Food;     private String cityFrom;     private String cityTo;      // Constructor     public Reservation() {}      // Getters and Setters     public String getFirstName() { return firstName; }     public void setFirstName(String firstName)     {         // this keyword refers to current instance itself         this.firstName = firstName;     }      // Getters and Setters     public String getLastName() { return lastName; }     public void setLastName(String lastName)     {         this.lastName = lastName;     }      // Getters and Setters     public String getGender() { return Gender; }     public void setGender(String gender)     {         Gender = gender;     }      // Getters and Setters     public String[] getFood() { return Food; }     public void setFood(String[] food) { Food = food; }     public String getCityFrom() { return cityFrom; }     public void setCityFrom(String cityFrom)     {         this.cityFrom = cityFrom;     }      // Getters and Setters     public String getCityTo() { return cityTo; }     public void setCityTo(String cityTo)     {         this.cityTo = cityTo;     } } 

Step 3: Create the controller class

File: ReservationController.java

Java
// Java Program to Illustrate ReservationController Class  package com.geeksforgeeks;  // Importing required classes import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping;  // Annotation @RequestMapping("/reservation") @Controller  // Class public class ReservationController {     @RequestMapping("/bookingForm")      // Method     public String bookingForm(Model model)     {         Reservation res = new Reservation();         model.addAttribute("reservation", res);          return "reservation-page";     }      // Annotation     @RequestMapping("/submitForm")     // Method     public String submitForm(@ModelAttribute("reservation")                              Reservation res)     {         return "confirmation-form";     } } 

Step 4: Provide the entry of controller in the web.xml file

File: web.xml

Java
<?xml version="1.0" encoding="UTF-8"?> <web-app>   <display-name>SpringMVC</display-name>    <servlet>       <servlet-name>spring</servlet-name>       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>       <load-on-startup>1</load-on-startup>     </servlet>   <servlet-mapping>       <servlet-name>spring</servlet-name>       <url-pattern>/</url-pattern>   </servlet-mapping>   </web-app> 

Step 5: Define the bean in the XML file

File: spring-servlet.xml

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         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd">      <!-- Add support for component scanning -->     <context:component-scan base-package="com.geeksforgeeks" />          <!--Add support for conversion, formatting and validation -->     <mvc:annotation-driven/>      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="prefix" value="/WEB-INF/jsp/"></property>         <property name="suffix" value=".jsp"></property>             </bean> </beans> 

Step 6: Create the requested page

File: index.jsp

HTML
<!DOCTYPE html> <html> <head>     <title>Railway Reservation System</title> </head> <body> <a href="reservation/bookingForm">GFG Railway Reservation System.</a> </body> </html> 

Step 7: Create the view components

File: reservation-page.jsp

HTML
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html> <html> <head>     <title>Reservation Form</title> </head> <h3>Railway Reservation Form</h3> <body>     <form:form action="submitForm" modelAttribute="reservation">         First name: <form:input path="firstName" />                 <br><br>         Last name: <form:input path="lastName" />         <br><br>         Gender:          Male<form:radiobutton path="Gender" value="Male"/>         Female<form:radiobutton path="Gender" value="Female"/>         <br><br>         Meals:         BreakFast<form:checkbox path="Food" value="BreakFast"/>         Lunch<form:checkbox path="Food" value="Lunch"/>         Dinner<form:checkbox path="Food" value="Dinner"/>         <br><br>         Leaving from: <form:select path="cityFrom">         <form:option value="Delhi" label="Delhi"/>         <form:option value="Noida" label="Noida"/>         <form:option value="Amritsar" label="Amritsar"/>         </form:select>         <br><br>         Going to: <form:select path="cityTo">         <form:option value="Mumbai" label="Mumbai"/>         <form:option value="Pune" label="Pune"/>         <form:option value="Nashik" label="Nashik"/>         </form:select>         <br><br>         <input type="submit" value="Submit" />     </form:form> </body> </html> 

File: confirmation-page.jsp

HTML
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <body>  <p>Geeksforgeeks reservation is confirmed successfully.</p>  First Name : ${reservation.firstName} <br> Last Name : ${reservation.lastName} <br> Gender: ${reservation.gender}<br> Meals:  <ul> <c:forEach var="meal" items="${reservation.food}"> <li>${meal}</li> </c:forEach> </ul> Leaving From : ${reservation.cityFrom} <br> Going To : ${reservation.cityTo} </body> </html> 


Output:

Click on the link and you will see the following output

Select other Listbox

At the last, this output will be shows


Next Article
Spring - MVC Listbox

S

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

Similar Reads

    Spring MVC - Iterating List on JSP using JSTL
    JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
    6 min read
    Two-Way Data Binding in Spring MVC with Example
    Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
    7 min read
    Spring MVC - Basic Example using JSTL
    JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
    8 min read
    Spring MVC @ModelAttribute Annotation with Example
    In Spring MVC, the @ModelAttribute annotation binds a method parameter or method return value to a named model attribute and then exposes it to a web view. It refers to the property of the Model object. For example, if we have a form with a form backing object that is called "Student" then we can ha
    8 min read
    Data Transfer Object (DTO) in Spring MVC with Example
    In Spring Framework, Data Transfer Object (DTO) is an object that carries data between processes. When you're working with a remote interface, each call is expensive. As a result, you need to reduce the number of calls. The solution is to create a Data Transfer Object that can hold all the data for
    7 min read
    Spring MVC - Capture and Display the Data from Registration Form
    This article is the continuation of this article Spring MVC - Create Registration Form using Form Tag Library, where we have successfully created a registration form using the Form Tag Library. Here in this article, we are going to explain how can we capture the data that are entered by the user and
    3 min read
    Spring MVC - Create Registration Form using Form Tag Library
    Spring Framework provides spring’s form tag library for JSP views in Spring’s Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags. These tags ar
    7 min read
    Data Binding in Spring MVC with Example
    Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
    8 min read
    Spring MVC - Pagination with Example
    We will be explaining how we can implement pagination in Spring MVC Application. This is required when we need to show a lot of data on pages. Suppose in an e-commerce site we have a lot of products but we can't show all of those on a single page, so we will show only 20 products on each page. This
    3 min read
    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
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