Sunday, September 15, 2019

Spring MVC Form Drop-Down List

The Spring MVC form drop-down list contains the list of elements. This tag generates an HTML select element. It allows data binding to the selected element.

Syntax

  1. <form:select path="name">  
Here are some other tags used to select the options.

option tag

This tag generates an HTML option tag. Each tag contains a value that can be selected by the user.

Syntax

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

options tag

This tag generates a list of HTML option tags. Each tag contains a list of elements selected by the user.

Syntax

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

Example of Spring MVC Form Drop-Down List

1. Add dependencies to pom.xml file.

  1.           <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-webmvc</artifactId>  
  5.     <version>5.1.1.RELEASE</version>  
  6. </dependency>  
  7.   
  8.     <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  9. <dependency>    
  10.     <groupId>javax.servlet</groupId>    
  11.     <artifactId>servlet-api</artifactId>    
  12.     <version>3.0-alpha-1</version>    
  13. </dependency>  
  14.   
  15.     <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->  
  16. <dependency>  
  17.     <groupId>javax.servlet</groupId>  
  18.     <artifactId>jstl</artifactId>  
  19.     <version>1.2</version>  
  20. </dependency>  
  21. <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->  
  22. <dependency>  
  23.     <groupId>org.apache.tomcat</groupId>  
  24.     <artifactId>tomcat-jasper</artifactId>  
  25.     <version>9.0.12</version>  
  26. </dependency>  

2. Create the bean class

Reservation.java
  1. package com.javatpoint;  
  2.   
  3. public class Reservation {  
  4.   
  5.     private String firstName;  
  6.     private String lastName;  
  7.     private String Gender;  
  8.     private String[] Food;  
  9.     private String cityFrom;  
  10.     private String cityTo;  
  11.     public Reservation()  
  12.     {         
  13.     }  
  14.     public String getFirstName() {  
  15.         return firstName;  
  16.     }  
  17.     public void setFirstName(String firstName) {  
  18.         this.firstName = firstName;  
  19.     }  
  20.     public String getLastName() {  
  21.         return lastName;  
  22.     }  
  23.     public void setLastName(String lastName) {  
  24.         this.lastName = lastName;  
  25.     }  
  26.     public String getGender() {  
  27.         return Gender;  
  28.     }  
  29.     public void setGender(String gender) {  
  30.         Gender = gender;  
  31.     }  
  32.     public String[] getFood() {  
  33.         return Food;  
  34.     }  
  35.     public void setFood(String[] food) {  
  36.         Food = food;  
  37.     }     
  38.     public String getCityFrom() {  
  39.     return cityFrom;  
  40. }  
  41. public void setCityFrom(String cityFrom) {  
  42.     this.cityFrom = cityFrom;  
  43. }  
  44. public String getCityTo() {  
  45.     return cityTo;  
  46. }  
  47. public void setCityTo(String cityTo) {  
  48.     this.cityTo = cityTo;  
  49. }     
  50. }  

3. Create the controller class

ReservationController.java
  1. package com.javatpoint;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.ui.Model;  
  4. import org.springframework.web.bind.annotation.ModelAttribute;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. @RequestMapping("/reservation")  
  8. @Controller  
  9. public class ReservationController {  
  10.     @RequestMapping("/bookingForm")  
  11. public String bookingForm(Model model)  
  12. {  
  13.       //create a reservation object   
  14.     Reservation res=new Reservation();  
  15.       //provide reservation object to the model   
  16.     model.addAttribute("reservation", res);  
  17.     return "reservation-page";  
  18. }  
  19. @RequestMapping("/submitForm")  
  20. public String submitForm(@ModelAttribute("reservation") Reservation res)  
  21. {  
  22.     return "confirmation-form";  
  23. }  
  24. }  

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

web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>SpringMVC</display-name>  
  4.    <servlet>    
  5.     <servlet-name>spring</servlet-name>    
  6.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  7.     <load-on-startup>1</load-on-startup>      
  8. </servlet>    
  9. <servlet-mapping>    
  10.     <servlet-name>spring</servlet-name>    
  11.     <url-pattern>/</url-pattern>    
  12. </servlet-mapping>    
  13. </web-app>  

5. Define the bean in the xml file

spring-servlet.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13.     <!-- Provide support for component scanning -->  
  14.     <context:component-scan base-package="com.javatpoint" />  
  15.     <!--Provide support for conversion, formatting and validation -->  
  16.     <mvc:annotation-driven/>  
  17.     <!-- Define Spring MVC view resolver -->  
  18.      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  19.         <property name="prefix" value="/WEB-INF/jsp/"></property>  
  20.         <property name="suffix" value=".jsp"></property>       
  21.      </bean>  
  22. </beans>  

6. Create the requested page

index.jsp
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Railway Registration Form</title>  
  5. </head>  
  6. <body>  
  7. <a href="reservation/bookingForm">Click here for reservation.</a>  
  8. </body>  
  9. </html>  

7. Create the view components

reservation-page.jsp
  1. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  2. <!DOCTYPE html>  
  3. <html>  
  4. <head>  
  5.     <title>Reservation Form</title>  
  6. </head>  
  7. <h3>Railway Reservation Form</h3>  
  8. <body>  
  9.     <form:form action="submitForm" modelAttribute="reservation">  
  10.         First name: <form:input path="firstName" />         
  11.         <br><br>  
  12.         Last name: <form:input path="lastName" />  
  13.         <br><br>  
  14.         Gender:   
  15.         Male<form:radiobutton path="Gender" value="Male"/>  
  16.         Female<form:radiobutton path="Gender" value="Female"/>  
  17.         <br><br>  
  18.         Meals:  
  19.         BreakFast<form:checkbox path="Food" value="BreakFast"/>  
  20.         Lunch<form:checkbox path="Food" value="Lunch"/>  
  21.         Dinner<form:checkbox path="Food" value="Dinner"/>  
  22.         <br><br>  
  23.         Leaving from: <form:select path="cityFrom">  
  24.         <form:option value="Ghaziabad" label="Ghaziabad"/>  
  25.         <form:option value="Modinagar" label="Modinagar"/>  
  26.         <form:option value="Meerut" label="Meerut"/>  
  27.         <form:option value="Amristar" label="Amristar"/>  
  28.         </form:select>  
  29.         <br><br>  
  30.         Going to: <form:select path="cityTo">  
  31.         <form:option value="Ghaziabad" label="Ghaziabad"/>  
  32.         <form:option value="Modinagar" label="Modinagar"/>  
  33.         <form:option value="Meerut" label="Meerut"/>  
  34.         <form:option value="Amristar" label="Amristar"/>  
  35.         </form:select>  
  36.         <br><br>  
  37.         <input type="submit" value="Submit" />  
  38.     </form:form>  
  39. </body>  
  40. </html>  
confirmation-page.jsp
  1. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  2. <!DOCTYPE html>  
  3. <html>  
  4. <body>  
  5. <p>Your reservation is confirmed successfully. Please, re-check the details.</p>  
  6. First Name : ${reservation.firstName} <br>  
  7. Last Name : ${reservation.lastName} <br>  
  8. Gender: ${reservation.gender}<br>  
  9. Meals:   
  10. <ul>  
  11. <c:forEach var="meal" items="${reservation.food}">  
  12. <li>${meal}</li>  
  13. </c:forEach>  
  14. </ul>  
  15. Leaving From : ${reservation.cityFrom} <br>  
  16. Going To : ${reservation.cityTo}  
  17. </body>  
  18. </html>  
Output:
Spring MVC Form Drop-Down List
Spring MVC Form Drop-Down List
Spring MVC Form Drop-Down List
Spring MVC Form Drop-Down List

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...