//타임리프
<div class="form-group">
<label for="category">카테고리</label>
<select name="category" id="category" class="form-control">
<option value="">카테고리 선택</option>
<option th:each="category : ${categoryNames}" th:value="${category.id}"
th:text="${category.categoryName}" > </option>
</select>
</div>
@PostMapping("/add")
public String itemPost( @ModelAttribute("form") ItemForm itemForm,
@RequestParam("categoryId") Long categoryId)
@GetMapping("/add")
public String ItemAdd(@ModelAttribute("form") ItemForm itemForm, Model model){
List<Category> allCategory = categoryService.findAllCategory();
log.info(allCategory.get(0).getCategoryName());
model.addAttribute("categoryNames", allCategory);
return "/buy/new";
}
@Getter
@Setter
public class ItemForm {
@NotEmpty
private String itemName;
@Range(min = 1000 , max = 200000)
private int price;
@Range(min = 1 , max = 200)
private int stackQuantity;
-> 추가
private Long categoryId;
public ItemForm() {
}
public ItemForm(String itemName, int price, int stackQuantity) {
this.itemName = itemName;
this.price = price;
this.stackQuantity = stackQuantity;
}
}
categoryId 를 ItemForm의 인스턴스 변수로 추가하여 값을 받아보기로 했다.
//타임리프
<div class="form-group">
<label for="categoryId">카테고리</label>
<select name="categoryId" id="categoryId" class="form-control">
<option value="">카테고리 선택</option>
<option th:each="category : ${categoryNames}" th:value="${category.id}"
th:text="${category.categoryName}" > </option>
</select>
</div>
이렇게 타임리프토 수정하고 나서 로그를 찍어보니 값이 제대로 출력되는 걸 확인할 수 있었다.
그럼.. RequestParam에서 문제인데..
a. 구글링 해서 찾아보니 RequestParam으로 값을 가져올 때 @RequestParam(value) 값이랑 thymleaf 의 name 값이랑 동일 해야된다는 걸 알 수 있었다.
엄청 기본적인 것을 이해 못하고 있었다.
타임리프와 기본적인 MVC 어노테이션을 공부하자