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.
EmployeeManager.java
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(); employee.setId(employeeList.size()+1); employee.setFirstName(e.getFirstName()); employee.setLastName(e.getLastName()); employeeList.add(employee); return employee; } } |
Employee.java
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.
EmployeeAddController.java
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; @Controller public class EmployeeAddController { @RequestMapping("/showEmployeeForm") public ModelAndView getEmployeeForm(){ ModelAndView mv = new ModelAndView("employeeAdd"); mv.addObject("employeeEntity", new Employee()); return mv; } @RequestMapping("/addEmployee") public ModelAndView addEmployee(@ModelAttribute Employee e){ ModelAndView mv = new ModelAndView("employeeList"); EmployeeManager employeeManager = new EmployeeManager(); employeeManager.addEmployee(e); 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
employeeAdd.jsp
<%@ 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"> <html> <body> <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"> </form:form> </body> </html> |
Add a new link that leads to add employee form.
index.jsp
<%@ 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"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 4 MVC - HelloWorld Index Page</title> </head> <body> <center> <h3> <a href="hello">Hello World</a> </h3> <h3> <a href="employee">Employee List</a> </h3> <h3> <a href="showEmployeeForm">Add Employee</a> </h3> </center> </body> </html> |
Result
Final file hierarchy in Navigator view.
Run the project now, and the web pages are the following:
Download
Source Code Download
Finally a tutorial that actually works!
Great.
How about the next one:
“Next – Form Validation” (no link yet?)
need to make EmployeeManager a singleton, otherwise when you add another employee, the list will be nuked and reset.
no link in next, 😀
Why do you use: EmployeeManager employeeManager = new EmployeeManager();
and not Injection?
Great tutorial…
Great!