πŸ”™λ’€λ‘œκ°€κΈ°

BookForm

package jpabook.jpashop.controller;

import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class BookForm {

    private Long id;
    private String name;
    private int price;
    private int stockQuantity;

    private String author;
    private String isbn;
}

ItemController

package jpabook.jpashop.controller;

import jpabook.jpashop.domain.item.Book;
import jpabook.jpashop.service.ItemService;
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.PostMapping;

@Controller
@RequiredArgsConstructor
public class ItemController {

    private final ItemService itemService;

    @GetMapping("/items/new")
    public String createForm(Model model) {
        model.addAttribute("form", new BookForm());
        return "items/createItemForm";
    }
    @PostMapping("/items/new")
    public String create(BookForm form){
        Book book = new Book();
        book.setName(form.getName());
        book.setPrice(form.getPrice());
        book.setStockQuantity(form.getStockQuantity());
        book.setAuthor(form.getAuthor());
        book.setIsbn(form.getIsbn());

        itemService.saveItem(book);
        return "redirect/items";
    }
}

μƒν’ˆ 등둝 λ·°(items/createItemForm.html)

<!DOCTYPE HTML>
<html xmlns:th="<http://www.thymeleaf.org>">
<head th:replace="fragments/header :: header"/>
<body>
<div class="container">
    <div th:replace="fragments/bodyHeader :: bodyHeader"/>
    <form th:action="@{/items/new}" th:object="${form}" method="post">
        <div class="form-group">
            <label th:for="name">μƒν’ˆλͺ…</label>
            <input type="text" th:field="*{name}" class="form-control"
                   placeholder="이름을 μž…λ ₯ν•˜μ„Έμš”">
        </div>
        <div class="form-group">
            <label th:for="price">가격</label>
            <input type="number" th:field="*{price}" class="form-control"
                   placeholder="가격을 μž…λ ₯ν•˜μ„Έμš”">
        </div>
        <div class="form-group">
            <label th:for="stockQuantity">μˆ˜λŸ‰</label>
            <input type="number" th:field="*{stockQuantity}" class="formcontrol" placeholder="μˆ˜λŸ‰μ„ μž…λ ₯ν•˜μ„Έμš”">
        </div>
        <div class="form-group">
            <label th:for="author">μ €μž</label>
            <input type="text" th:field="*{author}" class="form-control"
                   placeholder="μ €μžλ₯Ό μž…λ ₯ν•˜μ„Έμš”">
        </div>
        <div class="form-group">
            <label th:for="isbn">ISBN</label>
            <input type="text" th:field="*{isbn}" class="form-control"
                   placeholder="ISBN을 μž…λ ₯ν•˜μ„Έμš”">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
    <br/>
    <div th:replace="fragments/footer :: footer"/>
</div> <!-- /container -->
</body>
</html>

μƒν’ˆ 등둝