Advertisement
tungSfer

btth3

May 9th, 2022
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. @Autowired
  2.     ProductService productService;
  3.    
  4.     @GetMapping({"/", "/home"})
  5.     public String home() {
  6.         return "index";
  7.     }
  8.    
  9.     @GetMapping("/products")
  10.     public String getAllProduct(Model model) {
  11.         model.addAttribute("productList", productService.getAllProduct());
  12.         return "products";
  13.     }
  14.    
  15.     @GetMapping("/products/add")
  16.     public String addProductGet(Model model) {
  17.         model.addAttribute("product", new Product());
  18.         return "productInfo";
  19.     }
  20.    
  21.     @PostMapping("/products/add")
  22.     public String addProductPost(@Valid Product product , Errors errors) {
  23.         if (null != errors && errors.getErrorCount() > 0) {
  24.             productService.addProduct(product);
  25.             return "productInfo";
  26.         } else {
  27.             return "redirect:/products";
  28.         }
  29.     }
  30.     @GetMapping("/products/edit/{code}")
  31.     public String editProductGet(Model model, @PathVariable String code) {
  32.         Product product = productService.getProductByCode(code);
  33.         model.addAttribute("product", product);
  34.         return "productInfo";
  35.     }
  36.    
  37.     @PostMapping("/products/edit/{code}")
  38.     public String editProductPost(@PathVariable String code, @ModelAttribute Product product) {
  39.         productService.updateProduct(product, code);
  40.         return "redirect:/products";
  41.     }
  42.    
  43.     @GetMapping("/products/delete/{code}")
  44.     public String deleteProduct(@PathVariable String code) {
  45.         productService.deleteProduct(code);
  46.         return "redirect:/products";
  47.     }
  48.    
  49.     @GetMapping("/products/deleteConfirm/{code}")
  50.     public String deleteConfirm(Model model, @PathVariable String code) {
  51.         model.addAttribute("product", productService.getProductByCode(code));
  52.         return "deleteConfirm";
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement