Spring MVC Tutorial: Process Form Submission

Spring Tutorial Index Page
Previous – Dependency Injection
Next – Form Validation

This article shows how to process form in Spring. In this program, a form submission module will be added. Specifically, we will use web form to add a new employee and show the new employee list.

Step 1: Prepare Required Classes

Add addEmployee method to EmployeeManager.java. This method will be used to add new employee to the list.


package com.programcreek.helloworld.service;
import java.util.ArrayList;
import java.util.List;
import com.programcreek.helloworld.model.Employee;
public class EmployeeManager {
	private static List<Employee> employeeList;
	public EmployeeManager(){
		 employeeList = new ArrayList<Employee>();
		 employeeList.add(new Employee(1, "Mike", "Smith"));
		 employeeList.add(new Employee(2, "John", "Taylor"));
		 employeeList.add(new Employee(3, "Dave", "Wilson"));	 
	public List<Employee> getEmployeeList(){
		return employeeList;
	public Employee addEmployee(Employee e){
		Employee employee = new Employee();
		return employee;


package com.programcreek.helloworld.model;
public class Employee {
	private int id;
	private String lastName;
	private String firstName;
	public Employee(){
	public Employee(int id, String lastName, String firstName) {
		this.id = id;
		this.lastName = lastName;
		this.firstName = firstName;
	public int getId() {
		return id;
	public void setId(int id) {
		this.id = id;
	public String getLastName() {
		return lastName;
	public void setLastName(String lastName) {
		this.lastName = lastName;
	public String getFirstName() {
		return firstName;
	public void setFirstName(String firstName) {
		this.firstName = firstName;

Step 2: Create Controller

Create a new controller to handle form submission.


package com.programcreek.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.programcreek.helloworld.model.Employee;
import com.programcreek.helloworld.service.EmployeeManager;
public class EmployeeAddController {
	public ModelAndView getEmployeeForm(){
		ModelAndView mv = new ModelAndView("employeeAdd");
		mv.addObject("employeeEntity", new Employee());
		return mv;
	public ModelAndView addEmployee(@ModelAttribute Employee e){
		ModelAndView mv = new ModelAndView("employeeList");
		EmployeeManager employeeManager = new EmployeeManager();
		mv.addObject("employeeList", employeeManager.getEmployeeList());
		return mv;

The controller process two requests here: one lead the web page to the form and the other process form submission. Note @ModelAttribute maps the attributes of forms to an employee.

Step 3: Create Views


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
	<h1>Add Employee</h1>
	<form:form method="post"  modelAttribute="employeeEntity"  action="addEmployee">
		First Name:<form:input path="firstName"></form:input><br/>
		Last Name: <form:input path="lastName"></form:input><br/>
		<input type="submit" value="Submit">

Add a new link that leads to add employee form.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
			<a href="hello">Hello World</a>
			<a href="employee">Employee List</a>
			<a href="showEmployeeForm">Add Employee</a>


Final file hierarchy in Navigator view.

Run the project now, and the web pages are the following:

Index Page
index page

Employee Form Page

Employee List Page

Source Code Download

5 thoughts on “Spring MVC Tutorial: Process Form Submission”

  1. need to make EmployeeManager a singleton, otherwise when you add another employee, the list will be nuked and reset.

Leave a Comment