Spring MVC Form Handling-and concept of Backing bean in Spring MVC | JavaInUse



Spring MVC: Form handling



Overview

In the previous chapter we implemented a Add new Employee functionality using JSTL. Here we will implement the Add Employee Functionality using Spring Form Library. While using Spring Form Tag Library we will also make use of the Spring Backing Bean

Lets Begin

We will create Eclipse Maven project as follows-

For this approach we make use of Backing Beans. Here we will split the functionality of addItem method into 2 methods.
a. The first method show() will create the Backing Bean of Item and return it.
b. The second method processRequest(Employee emp) will process the addItem Request.

So the AddEmployeeController will be as follows-
package com.javainuse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
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(value = "/addNewEmployee", method = RequestMethod.GET)
	public ModelAndView show() {
		return new ModelAndView("/addEmployee.jsp", "emp", new Employee());
	}

	@RequestMapping(value = "/addNewEmployee", method = RequestMethod.POST)
	public ModelAndView processRequest(Employee emp, Errors result) {
		if (result.hasErrors()) {
			return new ModelAndView("/addEmployee.jsp", "emp", emp);
		}
		employeeService.addNewEmployee(emp);
		return new ModelAndView("/employee-added.jsp", "name", emp.getName());
	}

}
2. Next we write the addEmployee.jsp which will accept the Backing Bean object created by the controller. Also this jsp makes use of Spring Form Tag Library.
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee</title>
</head>
<body>
	<div id="addEmployee">
		<form:form action="addNewEmployee.do" method="post" commandName="emp">
			<p>
				<label>Enter Employee Id<fmt:message key="emp.empId" /></label>
				<form:input path="empId" />
			</p>
			<p>
				<label>Enter Name<fmt:message key="emp.name" /></label>
				<form:input path="name" />
			</p>
			<p>
				<label>Enter Type<fmt:message key="emp.designation" /></label>
				<form:input path="designation" />
			</p>
			<p>
				<label>Enter Price<fmt:message key="emp.salary" /></label>
				<form:input path="salary" />
			</p>
			<input type="submit" value="Add New Employee" />
		</form:form>
	</div>
</body>
</html>
Now open the browser and hit the URL http://localhost:8080/employee-management-system/addNewEmployee.do. We will see that the default value of 0.0 for Salary is visible.
Now enter the values as below and press submit.


Verify if the item has been correctly added to the inventory using the URL http://localhost:8080/employee-management-system/viewAllEmployees.do


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