/** * Performs search. * * @param owner * @param bindingResult * @param model * @return view name */ @RequestMapping(value = "/search", method = RequestMethod.GET) public String search( @ModelAttribute("owner") Owner owner, BindingResult bindingResult, Model model) { // allow parameterless GET request for /owners to return all records if (owner.getLastName() == null) { owner.setLastName(""); // empty string signifies broadest possible search } String viewName; // find owners by last name Collection<Owner> owners = this.clinic.findOwners(owner.getLastName()); if (owners.size() < 1) { // no owners found bindingResult.rejectValue("lastName", "notFound", "not found"); viewName = "owner/SearchForm"; } else if (owners.size() > 1) { // multiple owners found model.addAttribute("owners", owners); viewName = "owner/SearchResults"; } else { // 1 owner found owner = owners.iterator().next(); viewName = "redirect:/owner/" + owner.getId(); } model.addAttribute( "allErrors", BindingResultUtils.getErrorMessages(bindingResult, messageSource)); return viewName; }
/** Saves form data. */ @RequestMapping(value = "/save", method = RequestMethod.POST) public String save( @ModelAttribute("owner") Owner owner, BindingResult bindingResult, Model model) { new OwnerValidator().validate(owner, bindingResult); if (bindingResult.hasErrors()) { model.addAttribute( "allErrors", BindingResultUtils.getErrorMessages(bindingResult, messageSource)); return "owner/EditForm"; } clinic.storeOwner(owner); return "redirect:/owner/" + owner.getId(); }
@Override public Integer getId() { return delegate.getId(); }