diff --git a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java index c7e0eec5..be99c3b7 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java +++ b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java @@ -34,6 +34,10 @@ public void postHandle( if (modelAndView == null) { return; } + String viewName = modelAndView.getViewName(); + if (viewName != null && viewName.startsWith("redirect:")) { + return; + } HttpSession session = request.getSession(true); diff --git a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java index 70c6f206..bce29c80 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java +++ b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java @@ -2,6 +2,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -17,4 +18,9 @@ public WebMvcConfig(SessionInfoInterceptor sessionInfoInterceptor) { public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(sessionInfoInterceptor); } + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addRedirectViewController("/", "/catalog"); + } } diff --git a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java index 25d921df..dd1fc32c 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java +++ b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java @@ -18,9 +18,10 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller -@RequestMapping("/") +@RequestMapping("/catalog") public class CatalogController { private static final Logger log = LoggerFactory.getLogger(CatalogController.class); @@ -49,7 +50,7 @@ public String index( return "catalog/index"; } - @GetMapping("/catalog/details/{id}") + @GetMapping("/details/{id}") public String details(@PathVariable int id, Model model) { log.info("Now loading... /Catalog/Details?id={}", id); CatalogItem catalogItem = catalogService.findCatalogItem(id); @@ -63,7 +64,7 @@ public String details(@PathVariable int id, Model model) { return "catalog/details"; } - @GetMapping("/catalog/create") + @GetMapping("/create") public String createForm(Model model) { log.info("Now loading... /Catalog/Create"); populateDropdowns(model); @@ -72,8 +73,12 @@ public String createForm(Model model) { return "catalog/create"; } - @PostMapping("/catalog/create") - public String create(@Valid CatalogItem catalogItem, BindingResult result, Model model) { + @PostMapping("/create") + public String create( + @Valid CatalogItem catalogItem, + BindingResult result, + Model model, + RedirectAttributes redirectAttributes) { log.info("Now processing... /Catalog/Create?catalogItemName={}", catalogItem.getName()); if (result.hasErrors()) { populateDropdowns(model); @@ -82,10 +87,10 @@ public String create(@Valid CatalogItem catalogItem, BindingResult result, Model } catalogService.createCatalogItem(catalogItem); catalogMetrics.incrementItemsCreated(); - return "redirect:/"; + return "redirect:/catalog"; } - @GetMapping("/catalog/edit/{id}") + @GetMapping("/edit/{id}") public String editForm(@PathVariable int id, Model model) { log.info("Now loading... /Catalog/Edit?id={}", id); CatalogItem catalogItem = catalogService.findCatalogItem(id); @@ -99,9 +104,13 @@ public String editForm(@PathVariable int id, Model model) { return "catalog/edit"; } - @PostMapping("/catalog/edit/{id}") + @PostMapping("/edit/{id}") public String edit( - @PathVariable int id, @Valid CatalogItem catalogItem, BindingResult result, Model model) { + @PathVariable int id, + @Valid CatalogItem catalogItem, + BindingResult result, + Model model, + RedirectAttributes redirectAttributes) { log.info("Now processing... /Catalog/Edit?id={}", catalogItem.getId()); if (result.hasErrors()) { catalogItem.setPictureUri("/items/" + catalogItem.getId() + "/pic"); @@ -111,10 +120,10 @@ public String edit( } catalogService.updateCatalogItem(catalogItem); catalogMetrics.incrementItemsUpdated(); - return "redirect:/"; + return "redirect:/catalog"; } - @GetMapping("/catalog/delete/{id}") + @GetMapping("/delete/{id}") public String deleteForm(@PathVariable int id, Model model) { log.info("Now loading... /Catalog/Delete?id={}", id); CatalogItem catalogItem = catalogService.findCatalogItem(id); @@ -127,8 +136,8 @@ public String deleteForm(@PathVariable int id, Model model) { return "catalog/delete"; } - @PostMapping("/catalog/delete/{id}") - public String deleteConfirmed(@PathVariable int id) { + @PostMapping("/delete/{id}") + public String deleteConfirmed(@PathVariable int id, RedirectAttributes redirectAttributes) { log.info("Now processing... /Catalog/DeleteConfirmed?id={}", id); CatalogItem catalogItem = catalogService.findCatalogItem(id); if (catalogItem == null) { @@ -136,7 +145,7 @@ public String deleteConfirmed(@PathVariable int id) { } catalogService.removeCatalogItem(catalogItem); catalogMetrics.incrementItemsDeleted(); - return "redirect:/"; + return "redirect:/catalog"; } private void populateDropdowns(Model model) { diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html index ff513636..42c4ffbc 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html +++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html @@ -94,7 +94,7 @@

Create

- [ Cancel ] + [ Cancel ]
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html index e0daebb5..9a59eebc 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html +++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html @@ -44,7 +44,7 @@

Are you sure you want to delete this?

- [ Cancel ] + [ Cancel ]
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html index bad3d7e3..200bc282 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html +++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html @@ -43,7 +43,7 @@

Details

diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html index 379d4927..888e1325 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html +++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html @@ -99,7 +99,7 @@

Edit

- [ Cancel ] + [ Cancel ]
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html index 39d0eb4b..87f6c66c 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html +++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html @@ -55,7 +55,7 @@
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/error.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/error.html index 9ffdbe4e..2dff3c04 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/error.html +++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/error.html @@ -12,7 +12,7 @@

Error

An error occurred while processing your request.

- Back to catalog + Back to catalog
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html index 112067e1..904b90b8 100644 --- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html +++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html @@ -14,7 +14,7 @@