Data Binding in Spring MVC with Example

Last Updated : 10 Jun, 2026

Data Binding in Spring MVC is a mechanism that automatically maps HTTP request parameters (form data, query parameters, etc.) directly into Java objects, making it easier to capture and process user input on the server side. It is commonly used in applications like registration forms, login pages, and Google Forms where user input needs to be stored and processed efficiently.

  • It automatically binds form fields to Java object properties.
  • It removes the need to manually use request.getParameter() for each input.
  • It supports simple types, objects, and even collections for complex forms.

How Data Binding Works in Spring MVC ?

When a user submits a form in Spring MVC, the framework automatically converts the incoming request data into a Java object using data binding. This removes the need for manual parsing and makes request handling clean and efficient.

  • User fills the form in the UI (HTML/JSP) and submits it
  • Request is sent to the Spring MVC Controller
  • Spring automatically maps request parameters to Java object fields
  • The bound object is used for business logic processing
  • Finally, a response is returned back to the view (JSP/HTML page)

Steps to implements Data Binding In Spring MVC

Follow these steps to create a Student Registration Form and use Spring MVC Data Binding to automatically bind the submitted form data to a Student Java object for processing and display.

Step 1: Create a Spring Boot 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: SpringMVCDataBinding
  • Packaging: war

Click Finish.

Step 2: Add Required Dependencies

Add the following maven dependencies and plugin to your pom.xml file

XML
<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>6.2.8</version>
    </dependency>

    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>6.0.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>11.0.8</version>
    </dependency>

    <dependency>
        <groupId>jakarta.servlet.jsp.jstl</groupId>
        <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
        <version>3.0.0</version>
    </dependency>

</dependencies>

Note:
This example uses the traditional Spring MVC approach to demonstrate Data Binding. Therefore, files such as WebAppInitializer.java and WebConfig.java are required for configuring the DispatcherServlet and Spring MVC components.

If the same application is developed using Spring Boot, these configuration files are not needed because Spring Boot automatically configures the DispatcherServlet, View Resolver, and embedded Tomcat server, reducing boilerplate configuration.

Step 3: Configure DispatcherServlet

This class registers the DispatcherServlet and maps it to all incoming requests. DispatcherServlet acts as the front controller of the Spring MVC application.

Java
package com.gfg.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInitializer
        extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

Step 4: Create Spring MVC Configuration Class

This configuration enables Spring MVC annotations and configures the View Resolver for JSP pages.

Java
package com.gfg.config;

import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.gfg")
public class WebConfig {

    @Bean
    public InternalResourceViewResolver viewResolver() {

        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();

        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");

        return resolver;
    }
}

Step 5: Create Model Class

This JavaBean stores student information. Spring MVC binds form values directly to its properties.

Java
package com.gfg.model;

public class Student {

    private String name;
    private String email;
    private int age;

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Step 6: Create Controller Class

The @ModelAttribute annotation automatically binds request parameters to the Student object, demonstrating Spring MVC Data Binding.

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

@Controller
public class StudentController {

    @GetMapping("/")
    public String showForm(Model model) {

        model.addAttribute("student", new Student());
        return "register";
    }

    @PostMapping("/register")
    public String registerStudent(
            @ModelAttribute Student student,
            Model model) {

        model.addAttribute("student", student);

        return "success";
    }
}

Step 7: Create Registration Form

This form collects student information. The input field names must match the Student class property names for successful binding.

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

<html>
<head>
    <title>Student Registration</title>
</head>
<body>

<h2>Student Registration Form</h2>

<form action="register" method="post">

    Name:
    <input type="text" name="name"/>
    <br><br>

    Email:
    <input type="email" name="email"/>
    <br><br>

    Age:
    <input type="number" name="age"/>
    <br><br>

    <input type="submit" value="Register"/>

</form>

</body>
</html>

Step 8: Create Success Page

This page displays the data stored in the Student object after successful form submission.

C++
<%@ page contentType="text/html;charset=UTF-8"%>

<html>
<head>
    <title>Success</title>
</head>
<body>

<h2>Registration Successful</h2>

<p>Name: ${student.name}</p>
<p>Email: ${student.email}</p>
<p>Age: ${student.age}</p>

</body>
</html>

Step 9: Run the Application

  • Right-click the project.
  • Select Run As - Run on Server.
  • Choose Apache Tomcat Server.
  • Click Finish.

After that use the following URL to run your controller

http://localhost:8080/SpringMVCDataBinding/

Output:

Screenshot-2026-06-04-113226
RegistrationForm

User enters the required details and submits the form. The request is sent to the Spring MVC Controller, where Spring automatically binds the form data to the Student object using Data Binding.

Screenshot-2026-06-04-113247
RegistrationForm

After successful Data Binding, the populated Student object is passed to the controller. The controller processes the data and forwards it to the success page for display.

Screenshot-2026-06-04-113305
SuccessPage

Explanation: When the user submits the registration form, the entered data is sent to the Spring MVC Controller as an HTTP request. Spring MVC automatically performs Data Binding by mapping the request parameters to the corresponding fields of the Student object, making the data readily available for processing without manual extraction.

Comment