コード例 #1
0
  /*
   * 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";
  }