spring mvc : item save
This commit is contained in:
@@ -5,10 +5,7 @@ import com.example.itemservice.domain.item.ItemRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
@@ -34,6 +31,47 @@ public class BasicItemController {
|
||||
return "basic/item";
|
||||
}
|
||||
|
||||
@GetMapping("/add")
|
||||
public String addForm() {
|
||||
return "basic/addForm";
|
||||
}
|
||||
|
||||
// @PostMapping("/add")
|
||||
public String addItemV1(@RequestParam String itemName,
|
||||
@RequestParam int price,
|
||||
@RequestParam Integer quantity,
|
||||
Model model) {
|
||||
Item item = new Item();
|
||||
item.setItemName(itemName);
|
||||
item.setPrice(price);
|
||||
item.setQuantity(quantity);
|
||||
|
||||
itemRepository.save(item);
|
||||
|
||||
model.addAttribute("item", item);
|
||||
|
||||
return "basic/item";
|
||||
}
|
||||
|
||||
// @PostMapping("/add")
|
||||
public String addItemV2(@ModelAttribute("item") Item item, Model model) {
|
||||
itemRepository.save(item);
|
||||
// model.addAttribute("item", item);
|
||||
return "basic/item";
|
||||
}
|
||||
|
||||
// @PostMapping("/add")
|
||||
public String addItemV3(@ModelAttribute Item item) {
|
||||
itemRepository.save(item);
|
||||
return "basic/item";
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public String addItemV4(Item item) {
|
||||
itemRepository.save(item);
|
||||
return "basic/item";
|
||||
}
|
||||
|
||||
// 테스트용 데이터
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
|
||||
<style>
|
||||
.container {
|
||||
max-width: 560px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="py-5 text-center">
|
||||
<h2>상품 등록 폼</h2>
|
||||
</div>
|
||||
<h4 class="mb-3">상품 입력</h4>
|
||||
<form action="item.html" th:action="@{/basic/items/add}" method="post">
|
||||
<div>
|
||||
<label for="itemName">상품명</label>
|
||||
<input type="text" id="itemName" name="itemName" class="form-control" placeholder="이름을 입력하세요">
|
||||
</div>
|
||||
<div>
|
||||
<label for="price">가격</label>
|
||||
<input type="text" id="price" name="price" class="form-control"
|
||||
placeholder="가격을 입력하세요">
|
||||
</div>
|
||||
<div>
|
||||
<label for="quantity">수량</label>
|
||||
<input type="text" id="quantity" name="quantity" class="form-control" placeholder="수량을 입력하세요">
|
||||
</div>
|
||||
<hr class="my-4">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-primary btn-lg" type="submit">상품
|
||||
등록</button>
|
||||
</div>
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-secondary btn-lg"
|
||||
th:onclick="|location.href='@{/basic/items}'|"
|
||||
onclick="location.href='items.html'" type="button">취소</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div> <!-- /container -->
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user