@RequestMapping(value = "/create", method = RequestMethod.GET) public String create(ModelMap model) { model.addAttribute("computer", new Computer()); model.addAttribute("companies", companyService.findAll()); return "/create"; }
@RequestMapping(value = "/save", method = RequestMethod.POST) public String save( @ModelAttribute Computer computer, BindingResult result, SessionStatus status, ModelMap model, RedirectAttributes redirectAttributes) { computerValidator.validate(computer, result); if (result.hasErrors()) { model.addAttribute("computer", computer); model.addAttribute("companies", companyService.findAll()); model.addAttribute("result", result); return "/create"; } else { computer = computerService.create(computer); status.setComplete(); redirectAttributes.addFlashAttribute( "success", "Computer named " + computer.getName() + " has been created"); return "redirect:/computers"; } }
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST) public String update( @PathVariable Long id, @RequestParam(required = false) String name, @RequestParam(required = false) String introduced, @RequestParam(required = false) String discontinued, @RequestParam(required = false) String company, ModelMap model, RedirectAttributes redirectAttributes) { ComputerForm form = new ComputerForm(); form.setId(id); form.setName(name); form.setIntroduced(introduced); form.setDiscontinued(discontinued); form.setCompany(company); if (!form.isValid()) { model.addAttribute("form", form); model.addAttribute("companies", companyService.findAll()); return "/edit"; } else { Computer computer = form.toComputer(); computer = computerService.update(computer); redirectAttributes.addFlashAttribute( "success", "Computer with id " + computer.getId() + " has been updated"); return "redirect:/computers"; } }