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 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
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