Open In App

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




  <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>
      
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>
  
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.12</version>
</dependency>
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>servlet-api</artifactId>  
    <version>3.0-alpha-1</version>  
</dependency>
  
<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 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 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




<?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 version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="
  
    <!-- 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




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




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




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


Article Tags :