Open In App

Spring MVC – Text Box

To begin, make sure you have a working Eclipse IDE installed and follow the steps below to create a Spring Web Framework-based Dynamic Form-based Web Application.

Steps to create TextBox in Spring MVC:



  1. Create a SpringMVCTextField project in the com.geeksforgeeks package.
  2. Under the com.geeksforgeeks package, create the Java classes Registration and RegistrationController.
  3. Create two view files in the jsp subfolder: Registration-page.jsp and confirmation-page.jsp.
  4. The last stage is to construct the content of the source and configuration files, as well as to export the program, as described below.

Example Project

Project structure:



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

You can download the required dependencies from URLs given in the comments of the program.




         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.javatpoint</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>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>
      
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.12</version>
    </dependency>
  
  </dependencies>
  <build>
    <finalName>SpringMVC</finalName>
  </build>
</project>

Step 2. Create the bean class

The bean class includes the variables (along with setter and getter methods) that correspond to the form’s input field.

Registration.java




package com.geekforgeeks;
public class Registration 
{
    private String firstName;
    private String lastName;
      
    public Registration()
    {        
    }
    public String getFirstName() 
    {
        return firstName;
    }
    public void setFirstName(String firstName) 
    {
        this.firstName = firstName;
    }
    public String getLastName() 
    {
        return lastName;
    }
    public void setLastName(String lastName) 
    {
        this.lastName = lastName;
    }    
}

Step 3. Create the controller class

RegistrationController.java




package com.geekforgeeks;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
  
@RequestMapping("/Registration")
@Controller
public class RegistrationController 
{
    
  @RequestMapping("/bookingForm")
  public String bookingForm(Model model)
  {
      Registration res=new Registration();
      model.addAttribute("Registration", res);
      return "Registration-page";
  }
    
  @RequestMapping("/submitForm")
  public String submitForm(@ModelAttribute("Registration") Registration res)
  {
      return "confirmation-form";
  }
}

Step 4. Provide the entry of controller in the web.xml 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

spring-servlet.xml




<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="
      
      <!-- Provide support for component scanning -->
    <context:component-scan base-package="com.geekforgeeks" />
      
      <!--Provide support for conversion, formatting and validation -->
    <mvc:annotation-driven/>
      
      <!-- Define Spring MVC view resolver -->
     <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

index.jsp




<!DOCTYPE html>
<html>
<head>
    <title>Student Registration Form</title>
</head>
<body>
<a href="Registration/bookingForm">Click here for registration.</a>
</body>
</html>

Step 7. Create other view components

Registration-page.jsp




<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<h3>Student Registration Form</h3>
<body>
    <form:form action="submitForm" modelAttribute="Registration">
        First name: <form:input path="firstName" />        
        <br><br>
        Last name: <form:input path="lastName" />
        <br><br>
        <input type="submit" value="Submit" />    
    </form:form>
</body>
</html>

confirmation-page.jsp




<!DOCTYPE html>
<html>
<body>
    
<p>Your Registration is confirmed successfully.</p>
  
  First Name : ${Registration.firstName} <br>
  Last Name : ${Registration.lastName}
</body>
</html>

Output:

After clicking the “Click here for registration” link following page will be shown

Click the submit button now


Article Tags :