Spring - MVC Form Handling

Last Updated : 23 Apr, 2026

Spring MVC Form Handling allows developers to capture user input from web forms, bind it to Java objects, and process it using controller logic. It simplifies handling form data by integrating model binding, validation, and view rendering in a structured way.

  • Automatically maps form fields to Java objects using @ModelAttribute
  • Easily validate user input and display errors in the view
  • Clearly separates form logic (Controller), data (Model), and UI (View)

Steps to Create a Spring MVC Student Form in Eclipse IDE

A step-by-step guide to creating and handling forms in a Spring MVC application.

Step 1: Create Maven Web Project

Create a Maven project using maven-archetype-webapp in Eclipse/STS.

  • Enter GroupId and ArtifactId
  • Project is created with pom.xml and basic folder structure
  • Packaging type should be WAR for web applications

Step 2: Add Required Dependencies (pom.xml)

Add Spring MVC and related dependencies in pom.xml.

  • Add spring-webmvc, spring-context, spring-core
  • Add javax.servlet-api and jsp-api for servlet support
  • Add jstl for JSP tags.
  • Maven will automatically download and manage dependencies.
XML

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

  com.gfg
  SpringMvcStudentForm
  0.0.1-SNAPSHOT
  war
  
    1.8
  

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    

    
    
        org.springframework
        spring-context
        5.3.15
    
  
    
    
        javax.servlet.jsp.jstl
        jstl
        1.2
    

    
    
        org.springframework
        spring-core
        5.3.15
    

    
    
        org.springframework
        spring-web
        5.3.15
    

    
    
        org.springframework
        spring-webmvc
        5.3.15
    

    
    
        javax.servlet.jsp
        javax.servlet.jsp-api
        2.3.3
        provided
    
    
    
    
        org.springframework
        spring-orm
        5.3.15
    

  

  
    SpringMvcStudentForm
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

Step 3: Configure root-context.xml

Create root-context.xml inside /WEB-INF/spring/.

  • Used to define shared beans across the application
  • Keeps common configurations separate
  • Can remain minimal for small projects
XML

 xmlns="http://www.springframework.org/schema/beans/"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans/ 
                           http://www.springframework.org/schema/beans//spring-beans.xsd">        

Step 4: Configure web.xml

  • DispatcherServlet acts as front controller
  • / handles all requests
  • ContextLoaderListener loads Spring context at startup
  • Links root-context.xml with the application
XML

 xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html"
    xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html 
                        http://www.oracle.com/technetwork/java/index.html"
    version="4.0">
    
        org.springframework.web.context.ContextLoaderListener
    
    
        gfg
        org.springframework.web.servlet.DispatcherServlet
        1
    
    
        gfg
        /
    
    
        contextConfigLocation
        /WEB-INF/spring/root-context.xml
    

Step 5: Create Model Class (Student.java)

Create a POJO class representing form data.

  • Fields: name, id, password
  • Add constructors, getters, and setters
  • Acts as data carrier between view and controller
Java
package com.gfg.model;

public class Student {

    private String name;
    private String id;
    private String password;

    // needed to create a new instance via reflection by
    // your persistence framework.
    public Student() { super(); }

    // if you don't create an constructor then there is no
    // need to provide an empty constructor.
    public Student(String name, String id, String password)
    {
        super();
        this.name = name;
        this.id = id;
        this.password = password;
    }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public String getId() { return id; }

    public void setId(String id) { this.id = id; }

    public String getPassword() { return password; }

    public void setPassword(String password)
    {
        this.password = password;
    };

    @Override public String toString()
    {
        return String.format(
            "Student [name=%s, id=%s, password=%s]", name,
            id, password);
    }
}

The gfg-servlet.xml file (in /WEB-INF) is named after the servlet in web.xml and configures Spring MVC. It enables annotations, scans controllers, handles resources, and resolves JSP views.

XML

 xmlns="http://www.springframework.org/schema/mvc/"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans/"
    xmlns:context="http://www.springframework.org/schema/context/"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc/ 
                        http://www.springframework.org/schema/mvc//spring-mvc.xsd
                        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">

     mapping="/resources/**" location="/resources/" />
  
     />
  
     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         name="prefix" value="/WEB-INF/views/" />
    
  
     base-package="com.spring.controller" />

Step 6: Create Controller Class (StudentController.java)

Create controller to handle requests.

  • Annotate class with @Controller
  • Acts as bridge between model and view
  • Handles user input and business logic
Java
package com.gfg.controller;

import com.gfg.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class StudentController {

    @GetMapping(path = "/")
    public String studentLogin(Model model)
    {
        model.addAttribute(
            "var", "Please Enter Your Login Details");
        return "login.jsp";
    }

    @PostMapping
    public String
    submitLogin(Model model,
                @ModelAttribute("student") Student student)
    {
        if (student.getId() != null
            & student.getPassword() != null) {
            if (student.getId().equals("gfg")
                && student.getPassword().equals("123")) {

                model.addAttribute("var",
                                   student.getName());
                return "success.jsp";
            }
            else {

                model.addAttribute("error",
                                   "Invalid Details");
                return "login.jsp";
            }
        }
        else {
            model.addAttribute("error",
                               "Please enter Details");
            return "login.js[";
        }
    }
}

Step 7: Handle GET Request (Load Form Page)

Define method using @GetMapping("/").

  • Loads login page (login.jsp)
  • Adds initial message to model
  • Displays form to the user
HTML
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Login Formtitle>
head>
<body>
    <form:form name="submitForm" method="POST">

        <div align="center">
            <table>
                <tr>
                    <td>Student nametd>
                    <td><input type="text" name="name" />td>
                tr>
                <tr>
                    <td>User idtd>
                    <td><input type="text" name="id" />td>
                tr>
                <tr>
                    <td>Passwordtd>
                    <td><input type="password" name="password" />td>
                tr>
                <tr>
                    <td>td>
                    <td><input type="submit" value="Submit" />td>
                tr>
            table>
            <div style="color: red">${error}div>

        div>
    form:form>
body>
html>

After successful login the StudentController class will redirect the page to success.jsp file located in "/src/main/webapp/WEB-INF/views/success.jsp". This page has a very simple HTML written welcoming page.

HTML
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
    <head>
        <title>Success Formtitle>
    head>
    <body>
        <font color="562789" align="center"><h1>Student Welcome pageh1>font>
        <span>${var}span> You have successfully logged in.
        <font color="555555"><h1>Hope you have a great day !h1>font>
    body>
html>

After coding all classes and configuration file your project structure would look something like this:

Output: 

Now It's time to run your project in tomcat, check out this link if you need help running a tomcat server. After successfully running the tomcat server type this link "http://localhost:8080/SpringMvcStudentForm/" in your favorite browser. (user id - "gfg" and password - "123")

OutputOutput

So, we have created a very basic login form-based web application with Spring MVC and tested it locally in the tomcat server.

Comment