VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-mvc-form-handling/

⇱ Spring - MVC Form Handling - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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
👁 Image

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.

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

Step 4: Configure web.xml

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

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

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.

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

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

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.

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

👁 Image

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

👁 Output
👁 Output

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

Comment