DoR@Hee의 끄적끄적

JSP 글 리스트 만들기 본문

개발/WEB개발

JSP 글 리스트 만들기

DoR@Hee 2019. 1. 31. 17:41

VIEW - board_list.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import ="java.util.ArrayList" %>
<%@ page import ="com.javalec.ex.dto.*" %>
<%request.setCharacterEncoding("EUC-KR");%>
<jsp:useBean id="dto" class="com.javalec.ex.dto.BDto" scope="page"></jsp:useBean>
<%ArrayList<BDto> board_list = (ArrayList<BDto>)request.getAttribute("board"); %>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="style2.css?ver=1.6">
        <link href="https://fonts.googleapis.com/css?family=Gamja+Flower&amp;subset=korean" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Mukta" rel="stylesheet">
    </head>
    <body>
        <header>
            <div>
                <jsp:include page="header.jsp" flush="false" />
            </div>
        </header>
            <jsp:include page="aside_left.jsp" flush="false" />
            <jsp:include page="aside_right.jsp" flush = "false" />
            <div id="board_list_content">
            <p id="writer_font">게시글 목록</p>
                <table id="boradlist">
                    <thead>
                        <tr>
                            <th class="t_Number">
                                번호
                            </th>
                            <th class="t_Title">
                                제목
                            </th>
                            <th class="t_Writer">
                                작성자
                            </th>
                            <th class="t_Time">
                                날짜    
                            </th>
                            <th class="t_Hit">
                                조회수
                            </th>
                        </tr>
                    
                    </thead>
                    <tbody>
                    <% for(int i = 0; i < board_list.size(); i++) { %>
          
                        <tr>
                            <td class="t_Number">
                                <%=board_list.get(i).getbId() %>
                            </td>
                            <td class="t_Subject">
                                <a href="board_content_view.do?bId=<%=board_list.get(i).getbId()%>"><%=board_list.get(i).getbTitle() %></a>
                            </td>
                            <td class="t_Writer">
                                <%=board_list.get(i).getbNickname() %>
                            </td>
                            <td class="t_Time">
                                <%=board_list.get(i).getbDate() %>
                            </td>
                            <td class="t_Hit">
                                <%=board_list.get(i).getbHit() %>
                            </td>
                        </tr>
                    <% } %>
                    </tbody>
                </table>
                <a href="board_write_view.do"><button type="submit" id ="write_btn">글쓰기</button></a>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
            <footer>
                <jsp:include page="footer.jsp" flush = "false" />
            </footer>
    </body>
</html>
cs

MODEL - BDao.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
    public ArrayList<BDto> board_list(){
        ArrayList<BDto> dtos = new ArrayList<BDto>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;
        String query = "select bId, bNickname, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent from board order by bGroup desc, bStep asc";
        
        try { 
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            pstmt = con.prepareStatement(query);
            res = pstmt.executeQuery();
            
            while(res.next()) {
                int bId = res.getInt("bId");
                String bNickname = res.getString("bNickname");
                String bTitle = res.getString("bTitle");
                String bContent = res.getString("bContent");
                Date bDate = res.getDate("bDate");
                int bHit = res.getInt("bHit");
                int bGroup = res.getInt("bGroup");
                int bStep = res.getInt("bStep");
                int bIndent = res.getInt("bIndent");
                
                BDto dto =new BDto(bId,bNickname,bTitle,bContent,bDate,bHit,bGroup,bStep,bIndent);
                dtos.add(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(res != null) res.close();
                if(pstmt != null) pstmt.close();
                if(con != null) con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return dtos;
    }
cs

CONTROLLER

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    private void actiondo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        request.setCharacterEncoding("EUC-KR");
        
        String viewPage = null// 페이지 리다이렉트
        BCommand command = null// command 저장
        
        String url = request.getRequestURI(); //  ex)http://localhost/board/test.jsp >> board/test.jsp
        String conPath = request.getContextPath(); // ex) http://localhost/board/test.jsp >> /test.jsp
        String con = url.substring(conPath.length());
 
        if(con.equals("/board_list.do")) {
            command = new BListCommand();
            command.execute(request,response);
            viewPage = "board_list.jsp";
        }
        
        RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
        dispatcher.forward(request, response);
        
    }
cs


COMMAND

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.javalec.ex.command;
 
import java.util.ArrayList;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javalec.ex.dao.*;
import com.javalec.ex.dto.*;
 
public class BListCommand implements BCommand{
    public void execute(HttpServletRequest request, HttpServletResponse response) { 
        BDao dao = new BDao();
        ArrayList<BDto> dtos = dao.board_list();
        request.setAttribute("board", dtos);
    }
}
 
cs



메인페이지 게시판링크(board_list.do) > controller > command > model > view



'개발 > WEB개발' 카테고리의 다른 글

JSP 로그아웃 만들기  (0) 2019.01.31
JSP 글 보기 만들기  (0) 2019.01.31
JSP 게시판 글 쓰기 만들기  (0) 2019.01.31
JSP 회원가입 만들기  (0) 2019.01.31
JSP를 이용해서 회원로그인 만들기  (0) 2019.01.31
Comments