Search Tutorials


Spring MVC Tutorials | JavaInUse

Spring MVC: Add Employee Functionality without Spring Form Handling



Overview

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-
mvc3-16
Create a new addEmployee.jsp using JSP and JSTL Tag Library as follows-
Here the post method addNewEmployee.do is called on pressing the submit button.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee</title>
</head>
<body>
	<div id="addItem">
		<form action="addNewEmployee.do" 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" />
		</form>
	</div>
</body>
</html>

Now we will write a controller which will map to url addNewEmployee.do.
  
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;

@Controller
public class AddEmployeeController {

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping("/addNewEmployee")
	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);
		employeeService.addNewEmployee(employee);
		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;

@Component
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) {
		testEmployees.add(employee);

	}
}

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

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

click add.
mvc3-9

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


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.