/** * This method will be called on form submission, handling POST request for saving user in * database. It also validates the user input */ @RequestMapping( value = {"/newuser"}, method = RequestMethod.POST) public String saveUser(@Valid User user, BindingResult result, ModelMap model) { if (result.hasErrors()) { return "registration"; } /* * Preferred way to achieve uniqueness of field [sso] should be implementing custom @Unique annotation * and applying it on field [sso] of Model class [User]. * * 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 (!userService.isUserSSOUnique(user.getId(), user.getSsoId())) { FieldError ssoError = new FieldError( "user", "ssoId", messageSource.getMessage( "non.unique.ssoId", new String[] {user.getSsoId()}, Locale.getDefault())); result.addError(ssoError); return "registration"; } userService.saveUser(user); model.addAttribute("user", user); model.addAttribute( "success", "User " + user.getFirstName() + " " + user.getLastName() + " registered successfully"); // return "success"; return "registrationsuccess"; }