Sunday, September 15, 2019

Spring MVC Form Text Field

The Spring MVC form text field tag generates an HTML input tag using the bound value. By default, the type of the input tag is text.

Syntax

  1. <form:input path="name" />  
Here, path attribute binds the form field to the bean property.
The Spring MVC form tag library also provides other input types such as email, date, tel, etc.

For email:

  1. <form:input type=?email? path="email" />  

For date:

  1. <form:input type=?date? path="date" />  

Example of Spring MVC Form Text Field

Let's see an example to create a railway reservation form using form tag library.

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

Here, the bean class contains the variables (along setter and getter methods) corresponding to the input field exist in the form.
Reservation.java
  1. package com.javatpoint;  
  2.   
  3. public class Reservation {  
  4.   
  5.     private String firstName;  
  6.     private String lastName;  
  7.       
  8.     public Reservation()  
  9.     {         
  10.     }  
  11.     public String getFirstName() {  
  12.         return firstName;  
  13.     }  
  14.     public void setFirstName(String firstName) {  
  15.         this.firstName = firstName;  
  16.     }  
  17.     public String getLastName() {  
  18.         return lastName;  
  19.     }  
  20.     public void setLastName(String lastName) {  
  21.         this.lastName = lastName;  
  22.     }     
  23. }  

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. // @ModelAttribute binds form data to the object  
  21. public String submitForm(@ModelAttribute("reservation") Reservation res)  
  22. {  
  23.     return "confirmation-form";  
  24. }  
  25. }  

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 other 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.         <input type="submit" value="Submit" />      
  15.     </form:form>  
  16. </body>  
  17. </html>  

Note - The value passed with the @ModelAttribute annotation should be the same to the modelAttribute value present in the view page.

confirmation-page.jsp
  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4. <p>Your reservation is confirmed successfully. Please, re-check the details.</p>  
  5. First Name : ${reservation.firstName} <br>  
  6. Last Name : ${reservation.lastName}  
  7. </body>  
  8. </html>  
Output:
Spring MVC Form Text Field
Spring MVC Form Text Field
Spring MVC Form Text Field
Spring MVC Form Text Field

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