Spring MVC provides the <form:input> tag to create text input fields in web forms. It simplifies capturing user input and automatically binds the entered values to Java bean properties through Spring MVC's form handling mechanism.
- Used to collect single-line text input from users.
- Automatically binds form data to model objects.
- Reduces manual request parameter handling in controller classes.
Syntax
<form:input path="firstName"/>
- path: Specifies the model property to which the input value will be bound.
- id (optional): Defines a unique identifier for the text field.
To use Spring Form Tags in a JSP page, import the Spring Form Tag Library using the following directive:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
- prefix="form": Specifies the prefix used for Spring Form Tags.
- uri: Identifies the Spring Form Tag Library provided by Spring MVC.
Attributes in <form:input> Tag
The <form:input> tag provides various attributes to control the appearance, behavior, validation, and data binding of a text field in a Spring MVC form.
1. HTML Standard Attributes
These are global attributes that can be used with all HTML elements.
| Attribute Name | Description |
|---|---|
| accesskey | Specifies a shortcut key to focus the element. |
| id | Specifies a unique ID for the element. |
| lang | Specifies the language of the content. |
| tabindex | Specifies tab order of the element. |
| title | Specifies additional information about the element. |
| dir | Specifies the text direction. |
2. HTML Event Attributes
These attributes execute JavaScript functions when specific events occur.
| Attribute Name | Description |
|---|---|
| onblur | Triggered when the field loses focus. |
| onchange | Triggered when field value changes. |
| onclick | Triggered when the field is clicked. |
| ondblclick | Triggered when the field is double-clicked. |
| onfocus | Triggered when the field receives focus. |
| onkeydown | Triggered when a key is pressed. |
| onkeypress | Triggered when a key is pressed and released. |
| onkeyup | Triggered when a key is released. |
3. HTML Optional Attributes
These attributes modify the behavior and appearance of the text field.
| Attribute Name | Description |
|---|---|
| cssClass | Specifies CSS class for styling. |
| cssStyle | Applies inline CSS styles. |
| cssErrorClass | Applied when validation errors occur. |
| disabled | Disables the input field. |
| readonly | Makes the field read-only. |
| maxlength | Specifies maximum allowed characters. |
| size | Specifies visible width of the field. |
4. Other Attributes
These attributes are used for data binding and rendering.
| Attribute Name | Description |
|---|---|
| path | Binds the field to a model property. |
| htmlEscape | Enables or disables HTML escaping. |
Step by Step Implementation of Text Box in Spring MVC Application
Follow these steps to build a Spring MVC application using the <form:input> tag for collecting and displaying user information.
Step 1: Create a Maven Project
- Open STS IDE.
- Click File - New - Maven Project.
- Select Create a simple project (Select archetype ) and click Next.
Then Enter the following details:
- Group Id: com.gfg
- Artifact Id: SpringMVCTextField
- Packaging: war
Click Finish.
Step 2: Add Required Dependencies
Add the following maven dependencies and plugin to your pom.xml file.
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.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>
<!-- 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/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>
<!-- 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>
</dependencies>
<build>
<finalName>SpringMVC</finalName>
</build>
</project>
Below is the final project structure of the Spring MVC project after creating *.java and *.jsp files also.

Step 3: Create Model Class (Registration.java)
This bean class stores the user-entered first name and last name. Spring MVC automatically binds the form values to this model object using getter and setter methods.
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 4: Create Controller Class (RegistrationController.java)
This controller handles incoming requests, creates the model object, and processes form submissions. It controls navigation between JSP pages.
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 5: Configure Dispatcher Servlet (web.xml)
The web.xml file registers the Spring DispatcherServlet and maps all incoming requests to the Spring MVC framework for processing.
<?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 6: Configure Spring MVC (spring-servlet.xml)
This configuration file enables component scanning, annotation support, and view resolution. It allows Spring MVC to locate controllers and JSP pages.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
xmlns:xsi="https://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">
<!-- 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 7: Create Entry Page (index.jsp)
This page serves as the starting point of the application. It provides a link that redirects the user to the registration form.
<!DOCTYPE html>
<html>
<head>
<title>Student Registration Form</title>
</head>
<body>
<a href="Registration/bookingForm">Click here for registration.</a>
</body>
</html>
Step 8: Create Input Page (Registration-page.jsp)
This JSP page uses the <form:input> tag to collect the user's first name and last name. The entered values are bound to the Registration model object.
<%@ 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>
Step 9: Create Output Page (confirmation-page.jsp)
This JSP page displays the submitted values after successful form processing. The data is retrieved from the model using Expression Language (EL).
<!DOCTYPE html>
<html>
<body>
<p>Your Registration is confirmed successfully.</p>
First Name : ${Registration.firstName} <br>
Last Name : ${Registration.lastName}
</body>
</html>
Step 10: Run Your Application
- Right-click the project.
- Select Run As - Run on Server.
- Choose Apache Tomcat Server.
- Click Finish.
Open the following URL in the browser:
http://localhost:8080/SpringMVCTextField/index.jsp
Output:

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

Once we submit the form, the entered details will be displayed on the screen

Explanation: This Spring MVC application demonstrates the use of the <form:input> tag for collecting single-line user input. The entered values are automatically bound to the Registration model object, processed by the controller, and displayed on the confirmation page using Spring MVC data binding.
Advantages
- Simplifies binding form data to model objects.
- Reduces manual request parameter handling.
- Supports validation and error handling integration.
Limitations
- Suitable only for single-line text input.
- Requires proper Spring MVC configuration.
- Does not provide validation by default.