2019.02.20 Model2 : 게시판 만들기(6)

2019. 2. 20. 15:28JSP

이제 기존의 게시글을 수정하기 위해 설정하는 작업을 진행할 것이다.

마찬가지로 bbsUpdate.do를 연결하기 위해 web.xml에 작성해야 한다.



*****화면이 구현될 수 있게 마찬가지로 bbsUpdateController.java를 생성한다.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.bbs.controller;
 
import java.io.IOException;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.bbs.dao.bbsDao;
import com.bbs.dto.bbsDto;
 
public class bbsUpdateController extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        process(req,resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        process(req,resp);
    }
 
    // DB에 있는 행을 가져와서 수정하는 메소드를 호출하는 메소드
    private void process(HttpServletRequest req, HttpServletResponse resp) {
        String bbsId = req.getParameter("bbsId");
        bbsDao bDAO = bbsDao.getInstance();
        bbsDto bDTO = new bbsDto();
        bDTO = bDAO.selectById(bbsId);
 
        req.setAttribute("bbsUpdate", bDTO);
        RequestDispatcher rd = req.getRequestDispatcher("WEB-INF/bbsUpdate.jsp");
        try {
            rd.forward(req, resp);
        } catch (ServletException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
}
 
cs



① 화면에서 구현될 bbsUpdate.jsp를 생성한다. 여기서 표시될 내용은 기존의 데이트를 

가져야되므로  JSPL core 태그를 사용하여 읽어오는 작업을 추가한다.


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
</head>
<body style="background-color: #999999;">
    <div>
        <div align="center">게시판</div>
        <div align="center">
            <form method="post" action="bbsReplace.do">
            <input type="hidden" name="bbsId" value="${bbsUpdate.bbsId }">
                <table border=0px>
                    <tr>
                        <td><h6>카테고리</h6></td>
                        <td colspan="4"><select name="category">
                                <c:choose>
                                    <c:when test="${bbsUpdate.bbsCategory == 'math'}">
                                        <option value="math">수학</option>
                                    </c:when>
                                    <c:when test="${bbsUpdate.bbsCategory == 'enjoy'}">
                                        <option value="enjoy">여행</option>
                                    </c:when>
                                    <c:when test="${bbsUpdate.bbsCategory == 'pic'}">
                                        <option value="pic">사진</option>
                                    </c:when>
                                    <c:when test="${bbsUpdate.bbsCategory == 'java'}">
                                        <option value="java">자바</option>
                                    </c:when>
                                    <c:when test="${bbsUpdate.bbsCategory == 'pic'}">
                                        <option value="web">웹프로그래밍</option>
                                    </c:when>
                                    <c:when test="${bbsUpdate.bbsCategory == 'estate'}">
                                        <option value="estate">부동산</option>
                                    </c:when>
                                    <c:when test="${bbsUpdate.bbsCategory == 'food'}">
                                        <option value="food">음식</option>
                                    </c:when>
                                    <c:when test="${bbsUpdate.bbsCategory == 'common'}">
                                        <option value="common">상식</option>
                                    </c:when>
                                </c:choose>
                                <option value="math">수학</option>
                                <option value="enjoy">여행</option>
                                <option value="pic">사진</option>
                                <option value="java">자바</option>
                                <option value="web">웹프로그래밍</option>
                                <option value="estate">부동산</option>
                                <option value="food">음식</option>
                                <option value="common">상식</option>
                        </select></td>
                    <tr>
                        <td><h5>제목</h5></td>
                        <td colspan="4"><input name="title" type="text"
                            value="${bbsUpdate.bbsTitle}"></td>
                    </tr>
                    <tr>
                        <td><h5>내용</h5></td>
                        <td colspan="4"><textarea name="content" rows="20" cols="80">${bbsUpdate.bbsContent }</textarea>
                    </tr>
                    <tr>
                        <td colspan="3"></td>
                        <td colspan="2">
                            <button type="submit">보내기</button>
                        </td>
                    </tr>
                </table>
            </form>
            <div>
                <button>
                    <a href="bbs.do">게시판</a>
                </button>
            </div>
            <div>
                <button>
                    <a href="">처음으로</a>
                </button>
            </div>
        </div>
    </div>
 
</body>
</html>
cs



② bbsUpdate.jsp에서 form을 통해  bbsReplace.do이동 할수 있도록 마찬가지로 web.xml을 

통해 작성한다.



③ bbsReplace.do로 인해 작동하게될 bbsReplaceController.java를 작성한다.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.bbs.controller;
 
import java.io.IOException;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import com.bbs.dao.bbsDao;
import com.bbs.dto.bbsDto;
 
public class bbsReplaceController extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/bbsUpdate.jsp"); // 위의 명령문이 모두 실행되면 bbs.do로 이동
        rd.forward(req, resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        // post방식으로 받아왔기 때문에 utf-8선언->한글의 깨짐을 방지
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charest=UTF-8");
 
        // 입력값 가져오기
        String category = req.getParameter("category");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String bbsId = req.getParameter("bbsId");
        // bbsDto에 값을 입력하기 -> 받아온 값을 bbsDto에 갱신
        bbsDto bDto = new bbsDto();
        bDto.setBbsId(bbsId);
        bDto.setBbsCategory(category);
        bDto.setBbsTitle(title);
        bDto.setBbsContent(content);
        
        System.out.println(bDto.toString()); //bDto에 제대로작성도는지 확인 
        
        bbsDao bDao = bbsDao.getInstance(); // 입력된 dao의 값을 받아온다
        bDao.update(bDto); // 받아온 dao값을 dto의 insert 메소드를 통해 DB에 작성
        
    
// RequestDispatcher rd =req.getRequestDispatcher("bbs.do"); // 위의 명령문이 모두 실행되면 bbs.do로 이동
// rd.forward(req, resp);
// RequestDispatcher도 상관없이 변경되기는 하지만 주소가 변경되기 때문에
// sendRedirect방식을 사용한다.
resp.sendRedirect("bbs.do")   } }
 
cs



****************************

여기서,RequestDispatcher을 사용하면 bbs.do로 이동했을때  주소값이 변경된다.

그러므로, sendRedirect로 사용하여 직접 bbs.do를 호출한다.


변경화면 수정을 해서 bbs.do로 직접 호출된다.






④ bbsReplaceController.java에서 사용될 수정 메소드 update를 bbsDao.java에 추가한다.

    기존의 데이트를 갱신하는 SQL문이므로 UPDATE를 사용한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// bbsId값으로 DB의 해당하는 행을 수정하는 메소드
    public int update(bbsDto bbsDto) {
        con = this.getConnection();
        StringBuffer query = new StringBuffer(); // SQL문 작성을 위한 버퍼
        query.append("UPDATE bbs SET BBSCATEGORY=?,BBSTITLE=?,BBSCONTENT=? WHERE BBSID=?");
        try {
            pstmt = con.prepareStatement(query.toString()); // 나눠진 SQL문을 합치는 작업
            pstmt.setString(1, bbsDto.getBbsCategory()); // ?의 값을 순서에 맞게 삽입
            pstmt.setString(2, bbsDto.getBbsTitle());
            pstmt.setString(3, bbsDto.getBbsContent());
            pstmt.setString(4, bbsDto.getBbsId());
    
            result = pstmt.executeUpdate(); // 실직적인 DB에 작성하는 문장
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(con, pstmt, null);
        }
        return result;
    }
cs




==> 여기까지 진행했을 경우의 파일구성