From 4979ec4ad30c0361b0763aa9aecd689a956799a2 Mon Sep 17 00:00:00 2001 From: haerong22 Date: Fri, 16 Jul 2021 00:41:13 +0900 Subject: [PATCH] springboot_validation : errors.properties --- .../ValidationItemControllerV2.java | 35 ++++++++++++++++++- .../src/main/resources/application.properties | 3 +- .../src/main/resources/errors.properties | 4 +++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 springboot_validation/src/main/resources/errors.properties diff --git a/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV2.java b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV2.java index d27bfc5a..463be587 100644 --- a/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV2.java +++ b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV2.java @@ -76,7 +76,7 @@ public class ValidationItemControllerV2 { return "redirect:/validation/v2/items/{itemId}"; } - @PostMapping("/add") +// @PostMapping("/add") public String addItemV2(@ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) { // 검증 로직 @@ -108,6 +108,39 @@ public class ValidationItemControllerV2 { return "redirect:/validation/v2/items/{itemId}"; } + @PostMapping("/add") + public String addItemV3(@ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) { + + // 검증 로직 + if (!StringUtils.hasText(item.getItemName())) { + bindingResult.addError(new FieldError("item", "itemName", item.getItemName(), false, new String[]{"required.item.itemName"}, null, null)); + } + if (item.getPrice() == null || item.getPrice() < 1000 || item.getPrice() > 1000000) { + bindingResult.addError(new FieldError("item", "price", item.getPrice(), false, new String[]{"range.item.price"}, new Object[]{1000, 1000000}, null)); + } + if (item.getQuantity() == null || item.getQuantity() >= 9999) { + bindingResult.addError(new FieldError("item", "quantity", item.getQuantity(), false, new String[]{"max.item.quantity"}, new Object[]{9999}, null)); + } + if (item.getPrice() != null && item.getQuantity() != null ) { + int resultPrice = item.getPrice() * item.getQuantity(); + if (resultPrice < 10000) { + bindingResult.addError(new ObjectError("item", new String[]{"totalPriceMin"}, new Object[]{10000, resultPrice}, null; + } + } + + // 검증 실패시 + if (bindingResult.hasErrors()) { + return "validation/v2/addForm"; + } + + // 검증 성공시 + Item savedItem = itemRepository.save(item); + redirectAttributes.addAttribute("itemId", savedItem.getId()); + redirectAttributes.addAttribute("status", true); + return "redirect:/validation/v2/items/{itemId}"; + } + + @GetMapping("/{itemId}/edit") public String editForm(@PathVariable Long itemId, Model model) { Item item = itemRepository.findById(itemId); diff --git a/springboot_validation/src/main/resources/application.properties b/springboot_validation/src/main/resources/application.properties index 2780394d..a8874545 100644 --- a/springboot_validation/src/main/resources/application.properties +++ b/springboot_validation/src/main/resources/application.properties @@ -1 +1,2 @@ -#logging.level.org.apache.coyote.http11=debug \ No newline at end of file +#logging.level.org.apache.coyote.http11=debug +spring.messages.basename=messages, errors \ No newline at end of file diff --git a/springboot_validation/src/main/resources/errors.properties b/springboot_validation/src/main/resources/errors.properties new file mode 100644 index 00000000..25e05ba3 --- /dev/null +++ b/springboot_validation/src/main/resources/errors.properties @@ -0,0 +1,4 @@ +required.item.itemName=상품 이름은 필수입니다. +range.item.price=가격은 {0} ~ {1} 까지 허용합니다. +max.item.quantity=수량은 최대 {0} 까지 허용합니다. +totalPriceMin=가격 * 수량의 합은 {0}원 이상이어야 합니다. 현재 값 = {1} \ No newline at end of file