Close

Spring MVC - Simple Form Submission with Backing object

[Last Updated: Aug 11, 2020]

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.

@Controller
@RequestMapping("/register")
public class UserRegistrationController {

    @Autowired
    private UserService userService;

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

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


Create form page (user-registration.jsp)


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

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


Crete Response Page (registration-done.jsp)

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

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

http://localhost:8080/spring-form-submission/register

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

Simple Form Submission with Backing object Select All Download
  • spring-form-submission
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • UserRegistrationController.java
          • webapp
            • WEB-INF
              • views
        • test
          • java
            • com
              • logicbig
                • example


    /spring-form-submission/register


    On form submission

    See Also