@RequestMapping(value = "/edit-category/{categoryId}", method = RequestMethod.POST) public String doEditCategory( @ModelAttribute("category") @Valid Category category, Errors errors, @PathVariable String categoryId, @RequestParam("name") String name, @RequestParam("description") String description, Model model) { model.addAttribute("title", "Edit Category"); if (errors.hasErrors()) { model.addAttribute("category", category); return "edit-category"; } category = categoryService.getCategory(Integer.parseInt(HtmlUtils.htmlEscape(categoryId))); if (category == null) { return "redirect:manage-category.html"; } category.setDescription(description); category.setName(name); categoryService.updateCategory(category); model.addAttribute( "info", "<div class=\"info\">Succesfully Updated <p> <a href='../manage-category.html'>Go back to Management Page</a></div>"); return "edit-category"; }
@RequestMapping(value = "/create-category", method = RequestMethod.POST) public String createCategory( Model model, @ModelAttribute("category") @Valid Category category, Errors errors, @RequestParam("name") String name, @RequestParam("description") String description) { model.addAttribute("title", "Create Category"); Category newCategory = new Category(HtmlUtils.htmlEscape(name), HtmlUtils.htmlEscape(description)); if (errors.hasErrors()) { model.addAttribute("category", category); return "create-category"; } /* * What to do if there is no error * We need a DAO and Service to add category into the database */ try { categoryService.createCategory(newCategory); model.addAttribute( "success", "<div class=\"info\">Succesfully Created <p> <a href='manage-category.html'>Go back to Management Page</a></div>"); // Add success message category.setName(""); // Reset form field category.setDescription(""); // Reset form field } catch (EJBException e) { model.addAttribute("error", e.getMessage()); } return "create-category"; }
@RequestMapping(value = "/edit-category/{categoryId}", method = RequestMethod.GET) public String editCategory(@PathVariable String categoryId, Model model) { Category category = categoryService.getCategory(Integer.parseInt(HtmlUtils.htmlEscape(categoryId))); if (category == null) { return "redirect:manage-category.html"; } model.addAttribute("category", category); return "edit-category"; }
@RequestMapping(value = "/delete-category/{categoryId}", method = RequestMethod.GET) public String deleteCategory(@PathVariable String categoryId, Model model) { try { categoryService.deleteCategory(Integer.parseInt(HtmlUtils.htmlEscape(categoryId))); model.addAttribute( "info", "<div class=\"info\">Succesfully Deleted <p> <a href='../manage-category.html'>Go back to Management Page</a></div>"); } catch (EJBTransactionRolledbackException e) { model.addAttribute( "info", "<div class=\"info\">Category Doesn't Existed <p> <a href='../manage-category.html'>Go back to Management Page</a></div>"); } return "manage-category"; }
@RequestMapping(value = "/manage-category", method = RequestMethod.GET) public String manageCategory(Model model) { model.addAttribute("title", "Manage Category"); model.addAttribute("categories", categoryService.getAllCategory()); return "manage-category"; }