Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- LV2
- cors
- spring boot
- Spring
- 프로젝트
- springboot
- 해외봉사
- Dockerfile
- 로그인
- 세션로그인
- 회고
- 코딩테스트
- 서버 꺼짐
- 게시판
- openAI
- crud
- 네팔
- OOM
- docker
- 프로그래머스
- 쿠키로그인
- llm
- 부트스트랩
- mysql
- fastapi
- Lv.2
- 우테코
- 커밋 메시지
- Java
- 알고리즘
Archives
- Today
- Total
s00jin 님의 블로그
10. [게시판] Spring Boot/Mysql을 사용한 CRUD 게시판 만들기 | 게시판 댓글 기능 구현 본문
프로젝트/하고 싶은거 다해보는 내 사이트
10. [게시판] Spring Boot/Mysql을 사용한 CRUD 게시판 만들기 | 게시판 댓글 기능 구현
s00jin 2025. 8. 21. 16:12board_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;
}
위 코드들을 전부 작성 후 다시 서버를 시작하면 게시판 상세페이지에서 댓글을 작성할 수 있다.
