s00jin 님의 블로그

10. [게시판] Spring Boot/Mysql을 사용한 CRUD 게시판 만들기 | 게시판 댓글 기능 구현 본문

프로젝트/하고 싶은거 다해보는 내 사이트

10. [게시판] Spring Boot/Mysql을 사용한 CRUD 게시판 만들기 | 게시판 댓글 기능 구현

s00jin 2025. 8. 21. 16:12

board_detail.html 

<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" th:href="@{/style.css}">
</head>
<body>
    <!--게시물 제목 + 본문-->
    <h1 th:text="${board.subject}"></h1>
    <div th:text="${board.content}"></div>

    <!--게시물 댓글-->
    <h5 th:text="|${#lists.size(board.commentList)}개의 답변이 있습니다.|"></h5>
    <div>
      <ul>
        <li th:each="comment : ${board.commentList}" th:text="${comment.content}"></li>
      </ul>
    </div>

    <!--댓글 등록-->
    <form th:action="@{|/comment/${board.id}|}" method="post">
      <textarea name="content" id="content" rows="5"></textarea>
      <input type="submit" value="댓글 등록">
    </form>
</body>
</html>
#lists.size(객체)는 타임리프에서 제공하는 기능으로, 해당 객체의 길이를 반환
 

CommentController

package org.mySite.webBoard.controller;

import lombok.RequiredArgsConstructor;
import org.mySite.webBoard.domain.Board;
import org.mySite.webBoard.service.BoardService;
import org.mySite.webBoard.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequiredArgsConstructor
@Controller
public class CommentController {

    private final BoardService boardService;
    private final CommentService commentService;

    @PostMapping("/comment/{id}")
    public String createComment(Model model, @PathVariable("id") Long id, @RequestParam(value = "content") String content) {
        Board board = this.boardService.getBoard(id);
        this.commentService.create(board, content);
        return String.format("redirect:/board/detail/%s", id);
    }
}

 

@RequestParam(value="content") String content

  • question_detail.html에서 답변으로 입력한 내용(content)을 얻으려고 추가한 것
  • 템플릿의 답변 내용에 해당하는 <textarea>의 name 속성명이 content이므로 여기서도 변수명을 content로 사용

CommentService.java

package org.mySite.webBoard.service;

import lombok.RequiredArgsConstructor;
import org.mySite.webBoard.domain.Board;
import org.mySite.webBoard.domain.Comment;
import org.mySite.webBoard.repository.BoardRepository;
import org.mySite.webBoard.repository.CommentRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class CommentService {

    private final CommentRepository commentRepository;

    public void create(Board board, String content){
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setCreateDate(LocalDateTime.now());
        comment.setBoard(board);
        this.commentRepository.save(comment);
    }
}

style.css

textarea {
    width:100%;
}

input[type=submit] {
    margin-top:10px;
}

 

위 코드들을 전부 작성 후 다시 서버를 시작하면 게시판 상세페이지에서 댓글을 작성할 수 있다.