Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@GetMapping("/new")
public String form(Board board) {
    return "new";
}

// view
<div class="container">
    <div class="page-header">
        <h1>게시글 작성</h1>
    </div>
    <form action="#" th:action="@{/add}" th:object="${board}" method="post">
        <div class="form-group row">
            <label for="title" class="col-sm-2 col-form-label">제목</label>
            <div class="col-sm-10">
                <input th:field="*{title}" type="text" class="form-control" id="title" placeholder="제목">
            </div>
        </div>
        <div class="form-group row">
            <label for="title" class="col-sm-2 col-form-label">부제목</label>
            <div class="col-sm-10">
                <input th:field="*{subTitle}" type="text" class="form-control" id="subTitle" placeholder="부제목">
            </div>
        </div>
        <div class="form-group row">
            <label for="title" class="col-sm-2 col-form-label">내용</label>
            <div class="col-sm-10">
                <input th:field="*{content}" type="text" class="form-control" id="content" placeholder="내용">
            </div>
        </div>
        <input type="submit" value="작성">
    </form>
</div>

// add
@PostMapping("/add")
public String add(Board board, Model model) {
    board.setBoardType(BoardType.free);
    board.setCreatedDate(LocalDateTime.now());
    board.setUpdatedDate(LocalDateTime.now());
    Board saveBoard = boardRepository.save(board);
    model.addAttribute("board", boardService.findBoardByIdx(saveBoard.getIdx()));
    return "item";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment