diff --git a/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV3.java b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV3.java new file mode 100644 index 00000000..1ccba004 --- /dev/null +++ b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV3.java @@ -0,0 +1,73 @@ +package hello.itemservice.web.validation; + +import hello.itemservice.domain.item.Item; +import hello.itemservice.domain.item.ItemRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import java.util.List; + +@Controller +@RequestMapping("/validation/v3/items") +@RequiredArgsConstructor +public class ValidationItemControllerV3 { + + private final ItemRepository itemRepository; + + @GetMapping + public String items(Model model) { + List items = itemRepository.findAll(); + model.addAttribute("items", items); + return "validation/v3/items"; + } + + @GetMapping("/{itemId}") + public String item(@PathVariable long itemId, Model model) { + Item item = itemRepository.findById(itemId); + model.addAttribute("item", item); + return "validation/v3/item"; + } + + @GetMapping("/add") + public String addForm(Model model) { + model.addAttribute("item", new Item()); + return "validation/v3/addForm"; + } + + @PostMapping("/add") + public String addItem(@Validated @ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) { + + // 검증 실패시 + if (bindingResult.hasErrors()) { + return "validation/v3/addForm"; + } + + // 검증 성공시 + Item savedItem = itemRepository.save(item); + redirectAttributes.addAttribute("itemId", savedItem.getId()); + redirectAttributes.addAttribute("status", true); + return "redirect:/validation/v3/items/{itemId}"; + } + + + @GetMapping("/{itemId}/edit") + public String editForm(@PathVariable Long itemId, Model model) { + Item item = itemRepository.findById(itemId); + model.addAttribute("item", item); + return "validation/v3/editForm"; + } + + @PostMapping("/{itemId}/edit") + public String edit(@PathVariable Long itemId, @ModelAttribute Item item) { + itemRepository.update(itemId, item); + return "redirect:/validation/v3/items/{itemId}"; + } + +} + diff --git a/springboot_validation/src/main/resources/errors.properties b/springboot_validation/src/main/resources/errors.properties index 00920b56..8c6f873b 100644 --- a/springboot_validation/src/main/resources/errors.properties +++ b/springboot_validation/src/main/resources/errors.properties @@ -36,4 +36,9 @@ max= {0} 까지 허용합니다. #추가 typeMismatch.java.lang.Integer=숫자를 입력해주세요. -typeMismatch=타입 오류입니다. \ No newline at end of file +typeMismatch=타입 오류입니다. + +#Bean Validation 추가 +NotBlank={0} 공백X +Range={0}, {2} ~ {1} 허용 +Max={0}, 최대 {1} \ No newline at end of file diff --git a/springboot_validation/src/main/resources/templates/validation/v3/addForm.html b/springboot_validation/src/main/resources/templates/validation/v3/addForm.html new file mode 100644 index 00000000..b1d5f0de --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v3/addForm.html @@ -0,0 +1,73 @@ + + + + + + + + + +
+ +
+

상품 등록

+
+ +
+ +
+

글로벌 오류 메시지

+
+
+ + +
+ 상품명 오류 +
+
+
+ + +
+ 가격 오류 +
+
+
+ + +
+ 수량 오류 +
+
+ +
+ +
+
+ +
+
+ +
+
+ +
+ +
+ + \ No newline at end of file diff --git a/springboot_validation/src/main/resources/templates/validation/v3/editForm.html b/springboot_validation/src/main/resources/templates/validation/v3/editForm.html new file mode 100644 index 00000000..637ba550 --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v3/editForm.html @@ -0,0 +1,57 @@ + + + + + + + + + +
+ +
+

상품 수정

+
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+
+ +
+
+ +
+
+ +
+ +
+ + \ No newline at end of file diff --git a/springboot_validation/src/main/resources/templates/validation/v3/item.html b/springboot_validation/src/main/resources/templates/validation/v3/item.html new file mode 100644 index 00000000..2f847a82 --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v3/item.html @@ -0,0 +1,60 @@ + + + + + + + + + +
+ +
+

상품 상세

+
+ + +

+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+
+ +
+
+ +
+
+ +
+ + \ No newline at end of file diff --git a/springboot_validation/src/main/resources/templates/validation/v3/items.html b/springboot_validation/src/main/resources/templates/validation/v3/items.html new file mode 100644 index 00000000..5e535b0c --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v3/items.html @@ -0,0 +1,49 @@ + + + + + + + + +
+
+

상품 목록

+
+ +
+
+ +
+
+ +
+
+ + + + + + + + + + + + + + + + + +
ID상품명가격수량
회원id상품명1000010
+
+ +
+ + + \ No newline at end of file