Search Tutorials

Spring MVC Tutorials | JavaInUse

Spring MVC: Add Employee Functionality without Spring Form Handling


In the previous chapter we implemented a Simple SpringMVC program to display Employee information. Here we will modify the previous code to add functionality for adding new Employee information to the system.
Previously we used JSP and JSTL Tag Library in the displayAllEmployees.jsp. SpringMVC also provides its own Form Tag Library ,which has better functionality.
To show this we will implement the Add Employee Functionality first using normal JSP and JSTL Tag library as before.
In later chapter we will implement the Add Employee Functionality using Spring Form Library.

Lets Begin

We will create Eclipse Maven project as follows-
Create a new addEmployee.jsp using JSP and JSTL Tag Library as follows-
Here the post method is called on pressing the submit button.

<%@ taglib uri="" prefix="c"%>
<%@ taglib prefix="fmt" uri=""%>
<%@ taglib prefix="fn" uri=""%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee</title>
	<div id="addItem">
		<form action="" method="post">
			<label>Enter Employee Id</label><input type="text" name="empId" /><br>
			<label>Enter Employee Name</label><input type="text" name="name" /><br>
			<label>Enter Employee Designation</label><input type="text"
				name="designation" /><br> <label>Enter Employee Salary</label><input
				type="text" name="salary" /><br> <input type="submit"
				value="Add" />

Now we will write a controller which will map to url
package com.javainuse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.javainuse.domain.Employee;
import com.javainuse.service.EmployeeService;

public class AddEmployeeController {

	private EmployeeService employeeService;

	public ModelAndView addEmployee(@RequestParam("empId") String empId,
			@RequestParam("name") String name,
			@RequestParam("designation") String designation,
			@RequestParam("salary") String salary) {
		double salaryDouble = new Double(salary);
		Employee employee = new Employee(empId, name, designation, salaryDouble);
		return new ModelAndView("/employee-added.jsp", "name", name);

Here our EmployeeService is calling addNewEmployee method to add the new Employee info. We will implement this as follows-
package com.javainuse.service;

import java.util.List;
import com.javainuse.domain.Employee;

public interface EmployeeService {
	public List getAllEmployees();

	public void addNewEmployee(Employee employee);

package com.javainuse.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.javainuse.domain.Employee;

public class EmployeeServiceMockImpl implements EmployeeService {

	private List testEmployees = new ArrayList();

	public DisplayEmployeeServiceMockImpl() {
		testEmployees.add(new Employee("1", "emp1", "M1", 10000));
		testEmployees.add(new Employee("2", "emp2", "M2", 20000));
		testEmployees.add(new Employee("3", "emp3", "M3", 30000));
		testEmployees.add(new Employee("4", "emp4", "M4", 40000));
		testEmployees.add(new Employee("5", "emp5", "M5", 50000));
		testEmployees.add(new Employee("6", "emp6", "M6", 60000));

	public List getAllEmployees() {
		return new ArrayList(testEmployees);

	public void addNewEmployee(Employee employee) {


3. Finally we write the JSP page employee-added.jsp to show if the item is successfully added.
<%@ taglib uri="" prefix="c"%>
<%@ taglib prefix="fmt" uri=""%>
<%@ taglib prefix="fn" uri=""%>
<h2>The Employee  is now added in the System.</h2>

Now open the http://localhost:8080/employee-management-system/addEmployee.jsp and enter the values as below.

click add.

We can confirm if the element has been added by hitting the URl http://localhost:8080/employee-management-system/

Disadvantages of this approach-
  • If the salary field is not entered we get a null pointer exception.
  • No default value like 0.0 for Salary Field.
  • If wrong(non numeric) value is entered for salary, the other field values are also lost.

What Next?

In the next chapter will implement Spring MVC Form Handling- and concept of Backing bean in Spring MVC.