/* * This method will be called on form submission, handling POST request for * saving employee in database. It also validates the user input */ @RequestMapping( value = {"/new"}, method = RequestMethod.POST) public String saveEmployee(@Valid Employee employee, BindingResult result, ModelMap model) { if (result.hasErrors()) { return "registration"; } /* * Preferred way to achieve uniqueness of field [ssn] should be * implementing custom @Unique annotation and applying it on field [ssn] * of Model class [Employee]. * * Below mentioned peace of code [if block] is to demonstrate that you * can fill custom errors outside the validation framework as well while * still using internationalized messages. */ if (!service.isEmployeeSsnUnique(employee.getId(), employee.getSsn())) { FieldError ssnError = new FieldError( "employee", "ssn", messageSource.getMessage( "non.unique.ssn", new String[] {employee.getSsn()}, Locale.getDefault())); result.addError(ssnError); return "registration"; } employee.setId(service.getNext().intValue()); service.saveEmployee(employee); model.addAttribute("success", "Employee " + employee.getName() + " registered successfully"); return "success"; }
/* * This method will be called on form submission, handling POST request for * updating employee in database. It also validates the user input */ @RequestMapping( value = {"/edit-{ssn}-employee"}, method = RequestMethod.POST) public String updateEmployee( @Valid Employee employee, BindingResult result, ModelMap model, @PathVariable String ssn) { if (result.hasErrors()) { return "registration"; } if (!service.isEmployeeSsnUnique(employee.getId(), employee.getSsn())) { FieldError ssnError = new FieldError( "employee", "ssn", messageSource.getMessage( "non.unique.ssn", new String[] {employee.getSsn()}, Locale.getDefault())); result.addError(ssnError); return "registration"; } service.updateEmployee(employee); model.addAttribute("success", "Employee " + employee.getName() + " updated successfully"); return "success"; }
/* * This method will delete an employee by it's SSN value. */ @RequestMapping( value = {"/delete-{ssn}-employee"}, method = RequestMethod.GET) public String deleteEmployee(@PathVariable String ssn) { service.deleteEmployeeBySsn(ssn); return "redirect:/list"; }
/* * This method will provide the medium to update an existing employee. */ @RequestMapping( value = {"/edit-{ssn}-employee"}, method = RequestMethod.GET) public String editEmployee(@PathVariable String ssn, ModelMap model) { Employee employee = service.findEmployeeBySsn(ssn); model.addAttribute("employee", employee); model.addAttribute("edit", true); return "registration"; }
/* * This method will list all existing employees. */ @RequestMapping( value = {"/", "/list"}, method = RequestMethod.GET) public String listEmployees(ModelMap model) { List<Employee> employees = service.findAllEmployees(); model.addAttribute("employees", employees); return "allemployees"; }