// 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"; }
// handle screen for updating. Just display form for form edit. @RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET) public String updateForm(@PathVariable("id") Integer id, Model uiModel) { Category category = categoryService.findById(id); uiModel.addAttribute("category", category); resetCategories(uiModel, category, categoryService.findParents()); setBreadCrumb(uiModel, "/admin/categories", "Categories", "", "Update Category"); setUser(uiModel); addToLinks("Update Category", ""); return "categories/update"; }
// 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"; }
@RequestMapping(params = "form", method = RequestMethod.GET) public String createForm(Model uiModel) { Category category = new Category(); uiModel.addAttribute("category", category); resetCategories(uiModel, category, categoryService.findParents()); setBreadCrumb(uiModel, "/admin/categories", "Categories", "", "Create Category"); setUser(uiModel); addCategoryHomeLink(uiModel); addToLinks("New Category", ""); return "categories/create"; }
// handle /admin/categories @RequestMapping(method = RequestMethod.GET) public String list(Model uiModel) { logger.info("Listing categories"); List<Category> categories = categoryService.findAll(); uiModel.addAttribute("categories", categories); setBreadCrumb(uiModel, "/", "Home", "", "Categories"); setUser(uiModel); addCategoryHomeLink(uiModel); return "categories/list"; }
@RequestMapping( value = "/getListJson", method = RequestMethod.GET, produces = "application/json; charset=utf-8") @ResponseBody public String getProductsJson( @RequestParam(value = "filterscount", required = false) String filterscount, @RequestParam(value = "groupscount", required = false) String groupscount, @RequestParam(value = "pagenum", required = false) Integer pagenum, @RequestParam(value = "pagesize", required = false) Integer pagesize, @RequestParam(value = "recordstartindex", required = false) Integer recordstartindex, @RequestParam(value = "recordendindex", required = false) Integer recordendindex, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { // Constructs page request for current page // PageRequest pageRequest = null; // pageRequest = new PageRequest(pagenum, pagesize); List<Category> categories = categoryService.findAll(); List<TreeGrid> treeGrids = Utilities.createTreeGridForCategories(categories); String result = Utilities.jSonSerialization(treeGrids); return result; }