백엔드/Java

[개인 프로젝트] 게시판 만들기 5 - 공개, 비공개 기능

hawon6691 2025. 9. 28. 20:26
728x90

html 파일인 post, updateform, list, writeform과 java 파일 PostController 수정

 

PostDao.java 수정 (updatePost 함수에 sql 문이 키 값이 1로 고정되있어서 버그가 난 거였다.)

@Transactional
    public void updatePost(int postId, String title, String content, boolean active) {
        String sql = "update post set title = :title, content = :content, active = :active where postId = :postId";

        Post post = new Post();
        post.setPostId(postId);
        post.setTitle(title);
        post.setContent(content);
        post.setActive(active);
        SqlParameterSource params = new BeanPropertySqlParameterSource(post);
        jdbcTemplate.update(sql, params);
}

 

PostController.java 수정

@PostMapping("/write")
    public String write(@RequestParam("title") String title, @RequestParam("content") String content, @RequestParam(value = "isPublic", required = false, defaultValue = "false") Boolean isPublic, HttpSession session)
    {
        System.out.println("title : " + title);
        System.out.println("content : " + content);

        LoginInfo loginInfo = (LoginInfo)session.getAttribute("loginInfo");
        if(loginInfo == null) {
            return "redirect:/loginform";
        }

        postService.addPost(loginInfo.getUserId(), title, content, isPublic);

        return "redirect:/";
    }
    
 @PostMapping("/update")
    public String update(@RequestParam("postId") int postId, @RequestParam("title") String title, @RequestParam("content") String content, @RequestParam(value = "isPublic", required = false, defaultValue = "false") Boolean isPublic, HttpSession session) {
        LoginInfo loginInfo = (LoginInfo)session.getAttribute("loginInfo");
        if(loginInfo == null) return "redirect:/loginform";

        Post post = postService.getPost(postId, false);
        if(post.getUserId() != loginInfo.getUserId()) {
            return "redirect:/post?postId=" + postId;
        }

        postService.updatePost(postId, title, content, isPublic);
        return "redirect:/post?postId=" + postId;
    }

 

post.html 수정

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>게시물 상세보기</title>
</head>
<body>
    <h1 th:text="${post.title}"></h1>
    <div>글쓴이 : <span th:text="${post.name}"></span></div>
    <div>제목 : <span th:text="${post.title}"></span></div>
    <div>조회수 : <span th:text="${post.viewCount}"></span></div>
    <div>공개 : <span th:text="${post.isPublic}"></span></div>
    <div>작성일 : <span th:text="${post.updatedAt}"></span></div>
    <div>내용 : <span th:text="${post.content}"></span></div>

    <div><a href="/">목록보기</a></div>
    <div><a th:href="@{/updateform(postId=${post.postId})}" th:if="${(loginInfo != null) && (loginInfo.getUserId() == post.getUserId())}">수정하기</a></div>
    <div><a th:href="@{/delete(postId=${post.postId})}" th:if="${(loginInfo != null) && (loginInfo.getUserId() == post.getUserId())}">삭제하기</a></div>
    
</body>
</html>

updateform.html 수정

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>수정 폼</title>
</head>

<body>
    <h1>수정 폼</h1>
    <form action="/update" method="post">
        <input type="hidden" name="postId" th:value="${post.postId}">
        이름 : <span th:text="${loginInfo.name}"></span><br>
        제목 : <input type="text" name="title" th:value="${post.title}"><br>
        내용 : <br>
        <textarea name="content" cols="50" rows="5" th:value="${post.content}"></textarea><br>
        <input type="hidden" name="isPublic" value="false">
        공개 : <input type="checkbox" name="isPublic" value="true" th:checked="${post.isPublic} ? 'checked' : null"><br>
        <input type="submit" value="수정"> <input type="button" value="HOME" onclick="location.href='/'">
    </form>
</body>

</html>

list.html 수정

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>게시판 목록보기</title>
    <link rel="stylesheet" href="post.css">
</head>

<body>
    <section class="post">
        <div class="page-title">
            <div class="container">
                <h3>글 목록보기</h3>
            </div>
        </div>

        <div class="login-info">
            <div class="container">
                <span class="login-info_user" th:if="${loginInfo != null}">
                    <span th:text="${loginInfo.name}"></span>
                </span>
                <span class="login-info_logout" th:if="${loginInfo != null}">
                    <a href="/logout">로그아웃</a>
                </span>
                <span class="login-info_login" th:if="${loginInfo == null}">
                    <a href="/loginform">로그인</a>
                </span>
                <span class="login-info_join" th:if="${loginInfo == null}">
                    <a href="/userRegForm">회원가입</a>
                </span>
            </div>
        </div>

        <!-- post list area -->
        <div id="post-list">
            <div class="container">
                <table class="post-table">
                    <thead>
                        <tr>
                            <th scope="col" class="th-num">번호</th>
                            <th scope="col" class="th-title">제목</th>
                            <th scope="col" class="th-viewCount">조회수</th>
                            <th scope="col" class="th-name">작성자</th>
                            <th scope="col" class="th-date">등록일</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr th:each="post : ${list}" th:if="${post.isPublic == true} or ${loginInfo != null and loginInfo.userId == post.userId}">
                            <td th:text="${post.postId}"></td>
                            <th>
                                <a th:href="@{post(postId=${post.postId})}" th:text="${post.title}"></a>
                            </th>
                            <td th:text="${post.viewCount}"></td>
                            <td th:text="${post.name}"></td>
                            <td th:text="${post.updatedAt}"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="container">
                <a href="/writeForm" th:if="${loginInfo != null}">글쓰기</a>
            </div>
            <div class="container">
                <span th:each="n : ${#numbers.sequence(1, pageCount)}"><a th:href="@{/(page=${n})}" th:text="${n}"></a></span>
            </div>
        </div>
    </section>
</body>

</html>

writeform.html 수정

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>글쓰기 폼</title>
</head>

<body>
    <h1>글쓰기 폼</h1>
    <form action="/write" method="post">
        이름 : <span th:text="${loginInfo.name}"></span><br>
        제목 : <input type="text" name="title"><br>
        내용 : <br>
        <textarea name="content" cols="50" rows="5"></textarea><br>
        공개 : <input type="checkbox" name="isPublic"><br>
        <input type="submit" value="등록"> <input type="button" value="HOME" onclick="location.href='/'">
    </form>
</body>

</html>
728x90