[Spring] 게시판 만들기 12 – 게시판 카테고리 관리기

그럼 이어서 게시판 카테고리와 권한을 만든다.

우선 게시판 카테고리 생성및 수정

1. 페이지 만들

board_management.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>게시판 관리화면</title>

    <sec:csrfMetaTags />

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <link rel="stylesheet" href="/css/main.css">

</head>

<body>

    <header>

        <nav>

            <div class="menu">

                <div class="menu-item"><a href="${pageContext.request.contextPath}/">홈화면으로</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/userList">사용자관리화면</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/roleList">권한관리화면</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/boardList">게시판화면</a></div>

            </div>

        </nav>

    </header>

    <main>

        <nav>

            <ul>

                <li><a href="${pageContext.request.contextPath}/admin/boardList" id="boardList">게시판 목록</a></li>

                <li><a href="${pageContext.request.contextPath}/admin/addBoardForm" id="addBoard">게시판 추가</a></li>

            </ul>

        </nav>

        <section id="content">

            <h2>게시판 목록</h2>

                <sec:csrfInput />

                <!-- 게시판 목록을 테이블로 표시합니다. -->

                <table border="1">

                    <thead>

                        <tr>

                            <th>게시판 ID</th>

                            <th>이름</th>

                            <th>설명</th>

                            <th>관리</th>

                        </tr>

                    </thead>

                    <tbody>

                        <c:forEach var="board" items="${boards}">

                            <tr>

                                <td>${board.categoryId}</td>

                                <td>${board.categoryName}</td>

                                <td>${board.description}</td>

                                <td><a href="/admin/modifyBoardForm?categoryId=${board.categoryId}">수정</a></td>

                            </tr>

                        </c:forEach>

                    </tbody>

                </table>

        </section>

    </main>

    <script>

    </script>

</body>

</html>

board_add_form.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>게시판 관리화면</title>

    <sec:csrfMetaTags />

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <link rel="stylesheet" href="/css/main.css">

</head>

<body>

    <header>

        <nav>

            <div class="menu">

                <div class="menu-item"><a href="${pageContext.request.contextPath}/">홈화면으로</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/userList">사용자관리화면</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/roleList">권한관리화면</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/boardList">게시판화면</a></div>

            </div>

        </nav>

    </header>

    <main>

        <nav>

            <ul>

                <li><a href="${pageContext.request.contextPath}/admin/boardList" id="boardList">게시판 목록</a></li>

                <li><a href="${pageContext.request.contextPath}/admin/addBoardForm" id="addBoard">게시판 추가</a></li>

            </ul>

        </nav>

        <section id="content">카테고리 추가

<form action="/admin/addBoard" method="post">

            <sec:csrfInput />

    <div>

        <label for="categoryName">이름:</label>

        <input type="text" id="categoryName" name="categoryName" required>

    </div>

    <div>

        <label for="description">설명:</label>

        <input type="text" id="description" name="description" required>

    </div>

    <div>

        <button type="submit">등록</button>

    </div>

</form>

</section>

</main>

<script>

    $('#modifyBoard').click(function(){

        let categoryId = $(this).val();

        $.ajax({

            url: '/admin/modifyBoardForm',

            data : { categoryId : categoryId },

            method: 'GET',

            success: function(data){

                $('#content').html(data);

            }

        })

    })

</script>

</body>

</html>

board_modify_form.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>게시판 관리화면</title>

    <sec:csrfMetaTags />

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <link rel="stylesheet" href="/css/main.css">

</head>

<body>

    <header>

        <nav>

            <div class="menu">

                <div class="menu-item"><a href="${pageContext.request.contextPath}/">홈화면으로</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/userList">사용자관리화면</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/roleList">권한관리화면</a></div>

                <div class="menu-item"><a href="${pageContext.request.contextPath}/admin/boardList">게시판화면</a></div>

            </div>

        </nav>

    </header>

    <main>

        <nav>

            <ul>

                <li><a href="${pageContext.request.contextPath}/admin/boardList" id="boardList">게시판 목록</a></li>

                <li><a href="${pageContext.request.contextPath}/admin/addBoardForm" id="addBoard">게시판 추가</a></li>

            </ul>

        </nav>

        <section id="content">카테고리 수정

            category

<form action="/admin/modifyBoard" method="post">

            <sec:csrfInput />

    <div>

        <label for="categoryName">이름:</label>

        <input type="text" id="categoryName" name="categoryName" value="${category.categoryName}" required>

    </div>

    <div>

        <label for="description">설명:</label>

        <input type="text" id="description" name="description" value="${category.description}" required>

    </div>

    <input type="hidden" name="categoryId" value="${category.categoryId}">

    <div>

        <button type="submit">등록</button>

    </div>

</form>

<button id="deleteCategory" value="${category.categoryId}">게시판 삭제</button>

</section>

</main>

<script>

    $('#deleteCategory').click(function(){

            let categoryId = $(this).val();

            if(confirm('작성글이 전부 삭제됩니다. 삭제하시겠습니까?')){

                alert("미구현");

            } else {

                console.log("취소");

            }

        });

</script>

</body>

</html>

삭제는 게시글 지우기가 아깝고 좀 귀찮아서 일단 뒤로 미룸…

2. 컨트롤러 작성

board category 에 관해 작성한다.

adminController 에 이어서 하였음.

//Board

    @GetMapping("/boardList")

    public ModelAndView boardListPage() {

        String username = securityUtil.getCurrentUsername();

        mav.addObject("boards", postService.findAllCategory());

        mav.setViewName("admin/board_management");

        return mav;

    }

    @GetMapping("/addBoardForm")

    public String addBoardForm() {

        return "admin/board_add_form";

    }

    @PostMapping("/addBoard")

    public String registerBoard(@ModelAttribute Category category) {

        postService.addCategory(category);

        return "redirect:/admin/boardList";

    }

    @GetMapping("/modifyBoardForm")

    public ModelAndView getMethodName(HttpServletRequest request) {

        mav.addObject("category", postService.getCateogryInfo(request.getParameter("categoryId")));

        mav.setViewName("admin/board_modify_form");

        return mav;

    }

    @PostMapping("/modifyBoard")

    public String modifyBoard(@ModelAttribute Category category){

        postService.modifyBoardInfo(category);

        return "redirect:/admin/boardList";

    }

3. 서비스 작성

public List<Category> findCategory(String username) {

        return postDao.selectCategory(username);

    }

    public List<Category> findAllCategory() {

        return postDao.selectAllCategory();

    }

    public void addCategory(Category category) {

        postDao.insertCategory(category);

    }

    public String getCategoryName(String categoryId) {

        return postDao.selectCategoryName(categoryId);

    }

    public Category getCateogryInfo(String categoryId) {

        return postDao.selectCategoryInfo(categoryId);

    }

    public void modifyBoardInfo(Category category) {

        postDao.modifyBoardInfo(category);

    }

4. dao 작성

public List<Category> selectCategory(String username) {

        return sqlSession.selectList("PostMapper.selectCategory", username);

    }

    public void insertCategory(Category category) {

        sqlSession.insert("PostMapper.insertCateogry", category);

    }

    public String selectCategoryName(String categoryId) {

        return sqlSession.selectOne("PostMapper.selectCategoryName", Integer.parseInt(categoryId));

    }

    public Category selectCategoryInfo(String categoryId) {

        return sqlSession.selectOne("PostMapper.selectCategoryInfo", Integer.parseInt(categoryId));

    }

    public List<Category> selectAllCategory() {

        return sqlSession.selectList("PostMapper.selectAllCategory");

    }

5. SQL 작성

<select id="selectAllCategory" resultType="com.example.post.model.Category">

SELECT * from board_category1;

</select>

<select id="selectCategoryName" parameterType="int" resultType="String">

  SELECT categoryName FROM board_category1 WHERE categoryId = #{categoryId}

</select>

<select id="selectCategoryInfo" parameterType="int" resultType="com.example.post.model.Category">

  SELECT * FROM board_category1 WHERE categoryId = #{categoryId}

</select>

<update id="modifyBoardInfo" parameterType="com.example.post.model.Category">

  UPDATE board_category1 SET categoryName = #{categoryName}, description = #{description} WHERE categoryId = #{categoryId}

</update>

빠진 부분도있는데 이후 권한과 함께 다시포스팅하겠음.

우선 여기까지하고 다음권한에서 엮으며 마무리

Leave a Comment