본문 바로가기
Back-End/JSP & Thymeleaf

10.게시판 글수정 기능

by 두두리안 2020. 12. 25.
728x90

update.jsp

글수정 기능

update의 jsp 구성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="bbs.Bbs"%>
<%@ page import="bbs.BbsDAO"%>
<!DOCTYPE html>
<html>
<head>

</head>
<body>
<jsp:include page="head.jsp" flush="false" />
    <%
        String userID = null;
        if (session.getAttribute("userID") != null) {
            userID = (String) session.getAttribute("userID");
        }
        if (userID == null) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('로그인을 하세요.');");
            script.println("location.href='login.jsp'");
            script.println("</script>");
            script.close();
        }
        int bbsID = 0;
        if (request.getParameter("bbsID") != null) {
            bbsID = Integer.parseInt(request.getParameter("bbsID"));
        }
        if (bbsID == 0) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('유효하지 않은 글입니다.');");
            script.println("location.href='board.jsp';");
            script.println("</script>");
            script.close();
        }
        Bbs bbs = new BbsDAO().getBbs(bbsID);
        if (!userID.equals(bbs.getUserID())) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('권한이 없습니다.');");
            script.println("location.href='board.jsp';");
            script.println("</script>");
            script.close();
        }
    %>
    <div class="container">

            <form method="post" action="updateAction.jsp?bbsID=<%= bbsID %>">
                <table class="table table-bordered" style="text-align: center;">
                    <thead>
                        <tr>
                            <th colspan="2" style="background-color: #eeeeee; text-align: center;">게시판 글 수정 양식</th>
                        </tr>
                    </thead>
                    <tbody>
                            <tr>
                                <td colspan="2"><input type="text" class="form-control" placeholder="글 제목" name="bbsTitle" maxlength="50" value="<%= bbs.getBbsTitle() %>"></td>
                            </tr>
                            <tr>
                                <td colspan="2"><textarea class="form-control" name="bbsContent" placeholder="글 내용" maxlength="2048" style="height: 350px;"><%= bbs.getBbsContent() %></textarea></td>
                            </tr>
                    </tbody>
                </table>
                <input type="submit" class="btn btn-primary pull-right" value="글 수정">
            </form>

    </div>
    <jsp:include page="footer.jsp" flush="false" />
</body>
</html>

updateAction의 jsp 구성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="bbs.BbsDAO"%>
<%@ page import="bbs.Bbs"%>
<%@ page import="java.io.PrintWriter"%>
<%
    request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP 게시판 웹 사이트</title>
</head>
<body>
    <%
        String userID = null;
        if (session.getAttribute("userID") != null) {
            userID = (String) session.getAttribute("userID");
        }
        if (userID == null) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('로그인을 하세요.');");
            script.println("location.href='login.jsp'");
            script.println("</script>");
            script.close();
        }
        int bbsID = 0;
        if (request.getParameter("bbsID") != null) {
            bbsID = Integer.parseInt(request.getParameter("bbsID"));
        }
        if (bbsID == 0) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('유효하지 않은 글입니다.');");
            script.println("location.href='board.jsp';");
            script.println("</script>");
            script.close();
        }
        Bbs bbs = new BbsDAO().getBbs(bbsID);
        if (!userID.equals(bbs.getUserID())) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('권한이 없습니다.');");
            script.println("location.href='board.jsp';");
            script.println("</script>");
            script.close();
        } else {
            if (request.getParameter("bbsTitle") == null || request.getParameter("bbsTitle").equals("") || 
                    request.getParameter("bbsContent") == null || request.getParameter("bbsContent").equals("")) {
                PrintWriter script = response.getWriter();
                script.println("<script>");
                script.println("alert('입력이 안 된 사항이 있습니다.');");
                script.println("history.back();");
                script.println("</script>");
                script.close();
            } else {
                BbsDAO bbsDAO = new BbsDAO();
                int result = bbsDAO.update(bbsID, request.getParameter("bbsTitle"), request.getParameter("bbsContent"));
                if (result == -1) {
                    PrintWriter script = response.getWriter();
                    script.println("<script>");
                    script.println("alert('글쓰기에 실패하였습니다.');");
                    script.println("history.back();");
                    script.println("</script>");
                    script.close();
                } else {
                    PrintWriter script = response.getWriter();
                    script.println("<script>");
                    script.println("location.href = 'board.jsp';");
                    script.println("</script>");
                    script.close();
                }
            }
        }
    %>
</body>
</html>
728x90