2019.02.19 Model2 : 게시판 만들기(3)

2019. 2. 19. 14:59JSP


앞에서 DB의 TABLE에 입력값을 저장했다.  이제 저장된 값들을 불러와 WEB에서 출력하는 

작업을 할 것이다.

(DB의 정보를 WEB에서 출력하는 작업)



#1 먼저 DB의 내용을 출력할 수 있는 메소드를     bbsDao.java에서 추가한다.



****bbsDao.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
// DB TABLE 출력 메소드
    public List<bbsDto> selectAll(){ // 목록에 table을 표시하기위한 메소드
        List<bbsDto> bbsList = new ArrayList<>();
        con = this.getConnection();
        StringBuffer query = new StringBuffer();
        
        query.append("SELECT * FROM BBS ORDER BY BBSID DESC");
        try {
            pstmt = con.prepareStatement(query.toString());
            
            rs = pstmt.executeQuery();
            while(rs.next()) {
                bbsDto bDTO = new bbsDto();
                bDTO.setId(rs.getString("ID"));
                bDTO.setBbsId(rs.getString("BBSID"));
                bDTO.setBbsTitle(rs.getString("BBSTITLE"));
                bDTO.setBbsDate(rs.getString("BBSDATE"));
                bDTO.setBbsCategory(rs.getString("BBSCATEGORY"));
                bDTO.setBbsContent(rs.getString("BBSCONTENT"));
                bDTO.setBbsHit(rs.getString("BBSHIT"));
    
                bbsList.add(bDTO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bbsList;
    }//selectAll
cs


=> 목록을 저장하기 위한 List생성



#2 목록을 표시하기 위한 메소드를 사용하는     bbsController.java를 생성한다.



***bbsController.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
package com.bbs.controller;
 
import java.io.IOException;
import java.util.List;
 
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 bbsController 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);
    }
    
    private void process(HttpServletRequest req, HttpServletResponse resp){
        // 목록가져오기위한 메소드
        bbsDao bDAO = bbsDao.getInstance();
        List<bbsDto> list = bDAO.selectAll();
        
        req.setAttribute("list", list);
        RequestDispatcher rd =req.getRequestDispatcher("WEB-INF/bbs.jsp");
        try {
            rd.forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        }//process()
    
}
 
cs



#3 이제 목록을 가져오기 위한 기능적인 부분은     모두 완성되었다. 사용자에게 보여줄 

실직적인 화면 ,즉 jsp를 만든다.


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
<%@ 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>BBS</title>
</head>
<body>
    <div class="container">
        <div>
            <button><a href="bbs.do">게시판</a></button>
        </div>
        <div>
            <table>
                <thead>
                    <tr>
                        <th width="5%">번호</th>
                        <th width="10%">카테고리</th>
                        <th>작성자</th>
                        <th>제목</th>
                        <th width="30%"><a href="bbs.do?id=BBSCONTENT">내용</a></th>
                        <th width="20%"><a href="bbs.do?id=BBSDATE">날짜</a></th>
                        <th width="5%" align="left"><a href="bbs.do?id=BBSHIT">조회수</a></th>
                    </tr>
                </thead>
                <tbody>
                    <% //list에 저장된 내용을 출력-> bbsDto의 필드이름과 동일해야된다. %>
                    <c:forEach items="${list}" var="bbs">
                        <tr>
                            <td align="right">${bbs.bbsId}</td>
                            <td align="right">
                                <c:choose>
                                    <c:when test="${bbs.bbsCategory=='math'}">수학</c:when>
                                    <c:when test="${bbs.bbsCategory=='enjoy'}">여행</c:when>
                                    <c:when test="${bbs.bbsCategory=='pic'}">사진</c:when>
                                    <c:when test="${bbs.bbsCategory=='java'}">자바</c:when>
                                    <c:when test="${bbs.bbsCategory=='web'}">웹프로그래밍</c:when>
                                    <c:when test="${bbs.bbsCategory=='estate'}">부동산</c:when>
                                    <c:when test="${bbs.bbsCategory=='food'}">음식</c:when>
                                    <c:when test="${bbs.bbsCategory=='common'}">상식</c:when>
                                </c:choose>
                            </td>
                            <td align="right">${bbs.id}</td>
                            <td align="right">${bbs.bbsTitle}</td>
                            <td align="right">${bbs.bbsContent}</td>
                            <td align="right">${bbs.bbsDate}</td>
                            <td align="right">${bbs.bbsHit}</td>
                    </c:forEach>
                </tbody>
            </table>
        </div>
    </div>    
</body>
</html>
cs





****bbs.do를 열기위한 web.xml에서 작업을 추가해야된다.(앞으론 생략)



**** JSPL core 태그를 사용하기 위해 라이브러리 확인!!