// 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"; }
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()); }