[Spring Boot] 게시판 구현(3) - 상세 보기

우수빈·2023년 6월 28일
0

Spring Boot

목록 보기
4/10

PostController

@GetMapping("/detail")
    public void read(long id, Model model) {
        Post post = postService.read(id); // id로 POSTS 테이블에서 포스트를 검색.
        model.addAttribute("post", post); // 결과를 model에 저장. -> view로 전달.
    }

PostService

@Transactional(readOnly = true)
    public Post read(long id) {
        return postRepository.findById(id).orElseThrow();
    }

detail.html

<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleat/layout"
    layout:decorate="~{layout/base_layout}">
    
    <main layout:fragment="main">
        <div class="my-2 card" style="padding: 0 30px 0 30px;">
            <div class="card-body">
                <form>
                    <small class="card-text" th:text="|NO. ${post.id}|"></small>
                    <h2 class="card-title my-2" th:text="${post.title}"></h2>
                    <p class="card-text" th:text="|작성자: ${post.author}|"></p>
                    <hr />
                    <div class="my-2">
                        <label class="form-label" for="content">내용</label>
                        <textarea class="form-control" id="content" th:text="${post.content}" readonly></textarea>
                    </div>
                    <hr />
                    <p class="card-text text-end" 
                    th:text="|작성 시간: ${#temporals.format(post.createdTime, 'yyyy.MM.dd HH:mm:ss')} / 수정 시간: ${#temporals.format(post.modifiedTime, 'yyyy.MM.dd HH:mm:ss')}|"></p>
                </form>
                <div class="mt-5 d-grid gap-2 d-md-flex justify-content-md-center">
                    <a class="btn btn-outline-info" th:href="@{/post/modify?id={id} (id=${post.id})}">수정</a>
                </div>
            </div>
        </div>
    </main>

0개의 댓글