Spring MVC Form Handling Example


In this example, we will see how to handle a form submission in Spring MVC using a controller.


1.  Project structure



2.  Create the controller

File : UserController


The method initForm() is mapped to  GET requests on /index, it initializes the model and returns the name of index view.

The method submitForm() is mapped to  POST requests on /result path.

Using @ModelAttribute, the submitForm() method receives the User object populated by the form and then add it to the model, it also add a greeting message to the model. The data in the model will be rendered in the result view.


3. Create the model

File : User


4. Creates the views

File : index.jsp

The form fields must match the model attributes


File : result.jsp

It will display the data submitted by the index form


5. Spring config file


6. Test It

URL : http://localhost:8080/SpringMVCFormHandlingExample/index.htm



And we submit :



Download source code