본문 바로가기
JSP

JSP게시판 - 글목록 보기( BoardList )

by avvin 2019. 5. 9.

JSP게시판 - 글목록 보기( BoardList )



[게시글 쓰기] 작성 순서


글쓰기 > 글쓰기 처리 > 글쓰기 저장 > 전체게시글 보기


BoardWriteProc.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
<%@page import="model.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
 
    <!-- 게시글 작성한 데이터를 한번에 읽어들임 (1:1 맵핑) 없는 요소는 null로 들어감-->
    <%request.setCharacterEncoding("UTF-8"); %>
 
    <jsp:useBean id="boardbean" class="model.BoardBean">
        <jsp:setProperty name="boardbean" property="*" />
    </jsp:useBean>
 
    <%
        //데이터베이스쪽으로 빈클래스 넘겨주기
        //DAO객체 생성
        BoardDAO bdao = new BoardDAO();
        
        //데이터 저장(게시판 정보 삽입) 메서드 호출
        bdao.insertBoard(boardbean);
        
        //게시글 저장 후 전체 게시글 보기
        response.sendRedirect("BoardList.jsp");
        
    %>
</body>
</html>
cs



BoardList.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
57
58
59
60
61
<%@page import="model.BoardBean"%>
<%@page import="java.util.Vector"%>
<%@page import="model.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
 
<%
    //전체 게시글의 숫자, 제목을 jsp쪽으로 가져와야함
    BoardDAO bdao = new BoardDAO();
 
    //전체게시글을 리턴 받아주는 소스
    //(bean이라는 세트 여러개를 한꺼번에 받아오려면 리스트(벡터) 써야한다.)
    Vector<BoardBean> vec = bdao.getAllBoard();
 
    //받아온 데이터를 화면에 출력
%>
<center>
    <h2>전체 게시글 보기</h2>
    <table width="700" border="1" bgcolor=skyblue>
        <tr height="40">
            <td width="50" align="center">번호</td>
            <td width="320" align="center">제목</td>
            <td width="100" align="center">작성자</td>
            <td width="150" align="center">작성일</td>
            <td width="80" align="center">조회수</td>
        </tr>
 
        <%
            //데이터 출력
            for (int i = 0; i < vec.size(); i++) {
                //벡터에 저장돼있는 빈클래스를 하나씩 추출
 
                BoardBean bean = vec.get(i); 
                
                //글번호는 글스텝에 관계없이 부여되는 것이 보기좋으므로 i+1로 출력??
                //글제목에는 태그를 걸어준다.
                %>
                <tr height="40">
                <td width="50" align="center"> <%=i+1%> </td>
                <td width="320" align="left">
                <a href="BoardInfo.jsp?num=<%=bean.getNum()%>"> &nbsp;
                <%=bean.getSubject()%></a></td>
                <td width="100" align="center"><%=bean.getWriter()%></td>
                <td width="150" align="center"><%=bean.getReg_date()%></td>
                <td width="80" align="center"><%=bean.getReadcount()%></td>
            </tr>
    <
            }
    %>
    <tr height="40">
    <td align ="center" colspan="5">
    <input type="button" value="글쓰기" 
    onclick="location.href='BoardWriteForm.jsp'"></td>
    </tr>
    </table>
</center>
</body>
</html>
cs




BoardDAO.jsp 일부 [ getAllBoard()메서드 정의 ]

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
    //모든 게시글을 리턴해주는 메서드(카운터링 소스는 게시판 다 만든 후에 추가)
    public Vector<BoardBean> getAllBoard() {
        
        Vector<BoardBean> v = new Vector<BoardBean>();
        
        getCon();
        try {
            //쿼리 준비
            //글그룹 내림차순, 글스텝과 글레벨 오름차순으로 정렬하여 전체 칼럼 불러오기
            String sql = "select * from board order by ref desc, re_step asc, re_level asc";
            //쿼리 실행 객체 생성
            pstmt = con.prepareCall(sql);
            //쿼리 실행
            rs = pstmt.executeQuery();
            //데이터 개수가 몇개인지 모르므로 반복문 이용하여 데이터 추출
            //ResultSet에서 데이터를 꺼낼때에는 rs의 getType(Index)로 꺼내고
            //bean의 setter(꺼낸 데이터)로  패키징
            
            while(rs.next()) {
                //데이터를 패키징
                BoardBean bean = new BoardBean();
                
                bean.setNum(rs.getInt(1));
                bean.setWriter(rs.getString(2));
                bean.setEmail(rs.getString(3));
                bean.setSubject(rs.getString(4));
                bean.setPassword(rs.getString(5));
                //ResultSet은 getDate()메서드도 제공한다
                //byte[]타입으로 리턴되므로 toSting()처리해준다.
                bean.setReg_date(rs.getDate(6).toString());
                bean.setRef(rs.getInt(7));
                bean.setRe_step(rs.getInt(8));
                bean.setRe_level(rs.getInt(9));
                bean.setReadcount(rs.getInt(10));
                bean.setContent(rs.getString(11));
            
                //패키징한 데이터를 vector에 add
                v.add(bean);
                
            }
            con.close();
                    
                    
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return v;
    }
cs






'JSP' 카테고리의 다른 글

HTML 기본  (0) 2019.04.28