// create new category, save to database @RequestMapping(params = "form", method = RequestMethod.POST) public String create( @Valid Category category, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes, Locale locale) { logger.info("Creating category"); if (bindingResult.hasErrors()) { uiModel.addAttribute( "message", new Message( "error", messageSource.getMessage("category_save_fail", new Object[] {}, locale))); uiModel.addAttribute("category", category); addCategoryHomeLink(uiModel); resetCategories(uiModel, category, categoryService.findParents()); return "categories/create"; } uiModel.asMap().clear(); redirectAttributes.addFlashAttribute( "message", new Message( "success", messageSource.getMessage("category_save_success", new Object[] {}, locale))); String parentCategoryId = category.getParentCategoryId(); if (parentCategoryId != null && !parentCategoryId.equals("")) category.setParentCategory(categoryService.findById(Integer.valueOf(parentCategoryId))); categoryService.save(category); return "redirect:/admin/categories/" + UrlUtil.encodeUrlPathSegment(category.getCategoryId().toString(), httpServletRequest) + "?form"; }
// update an existing group, save to database @RequestMapping(value = "/{id}", params = "form", method = RequestMethod.POST) @Transactional public String update( @ModelAttribute("category") Category category, @PathVariable Integer id, Model uiModel, HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes, Locale locale, BindingResult bindingResult) { logger.info("Updating category"); Set<ConstraintViolation<Category>> violations = validator.validate(category); for (ConstraintViolation<Category> violation : violations) { String propertyPath = violation.getPropertyPath().toString(); String message = violation.getMessage(); // Add JSR-303 errors to BindingResult // This allows Spring to display them in view via a FieldError bindingResult.addError( new FieldError("user", propertyPath, "Invalid " + propertyPath + "(" + message + ")")); } if (bindingResult.hasErrors()) { uiModel.addAttribute( "message", new Message( "error", messageSource.getMessage("category_save_fail", new Object[] {}, locale))); uiModel.addAttribute("category", category); addCategoryHomeLink(uiModel); resetCategories(uiModel, category, categoryService.findParents()); return "categories/update"; } uiModel.asMap().clear(); category.setCategoryId(id); redirectAttributes.addFlashAttribute( "message", new Message( "success", messageSource.getMessage("category_save_success", new Object[] {}, locale))); uiModel.addAttribute("category", category); resetCategories(uiModel, category, categoryService.findParents()); String parentCategoryId = category.getParentCategoryId(); if (parentCategoryId != null && !parentCategoryId.equals("")) category.setParentCategory(categoryService.findById(Integer.valueOf(parentCategoryId))); categoryService.save(category); return "categories/update"; }
private void resetCategories(Model uiModel, Category category, List<Category> categories) { uiModel.addAttribute("parentCategories", categories); Category parentCategory = category.getParentCategory(); if (parentCategory != null) category.setParentCategoryId(parentCategory.getCategoryId().toString()); }