Spring MVC - Simple Form Submission

[Updated: Feb 28, 2016, Created: Feb 22, 2016]

In this example we are going to demonstrate a simple form submission using spring implicit data binding.

Create Form backing Object

public class User {
    private Long id;
    private String name;
    private String password;
    private String emailAddress;

    //getters and setters

Create the controller

We are creating two handler methods one with RequestMethod.GET to response back with an empty registration form and other one with RequestMethod.POST to handle the form submission. Just like Java Objects binding examples, we don't have to use any @ModelAttribute annotation because Spring implicit data binding will map the form fields to User object fields matched by names.

public class UserRegistrationController {

    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String handleGetRequest () {
        return "user-registration";

    @RequestMapping(method = RequestMethod.POST)
    public String handlePostRequest (User user, Model model) {
        return "registration-done";

Create form page (user-registration.jsp)

<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"

<h3> Registration Form <h3>
<form action="register" method="post" >
                  Name <input type="text" name="name" />
         Email address <input type="text" name="emailAddress" />
              Password <input type="password" name="password" />
                        <input type="submit" value="Submit" />

Crete Response Page (registration-done.jsp)

<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"
<h3> Registration done </h3>
<p>User Name: ${}

Example Project

To test controllers run the unit tests in RegistrationControllerTest.

Or you can run the app using embedded tomcat:

mvn  clean install tomcat7:run-war

Use this URL in your browser :


Dependencies and Technologies Used:

  • Spring Web MVC 4.2.4.RELEASE: Spring Web MVC.
  • Spring TestContext Framework 4.2.4.RELEASE: Spring TestContext Framework.
  • Java Servlet API 3.0.1
  • javax.servlet:jstl 1.2
  • JUnit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.0.4

Spring Form Submission Example Select All Download
  • spring-form-submission
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp
          • WEB-INF
            • views
      • test
        • java
          • com
            • logicbig
              • example


On form submission

See Also