본문 바로가기
매일코딩/Spring

26.스프링프로젝트 - 페이징 처리

by 인생여희 2016. 11. 17.
반응형





1. list page


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%-- <%@ page session="false"%> --%>
<c:set var="path" value="${pageContext.request.contextPath}"></c:set> 
 
 
 
 
<jsp:include page="boardHeader.jsp" flush="false"/>
 
 
                    <h1 id="community"><div>COMMUNITY</div></h1>
          <article>
              <h2>게시판</h2>
              
 
    
    <table border="1" align="center" class="tbl_type" WIDTH=50% table-layout: fixed>
        <tr>
            <th  nowrap WIDTH="40">번호</th>
            <th  nowrap WIDTH="100">이름</th>
            <th  nowrap WIDTH="250">제목</th>
            <th  nowrap WIDTH="100">날짜</th>
            <th  nowrap WIDTH="40">조회</th>
            <th  nowrap WIDTH="70">첨부파일</th>
            <th  nowrap WIDTH="70">다운로드</th>
        </tr>
 
 
        <c:forEach var="row" items="${list}">
            <tr>
                <td>${row.idx }</td>
                <td>${row.username }</td>
                <td>
                
                <!-- 답변 들여쓰기 -->
                <c:forEach begin="1" end="${row.depth }">
                &nbsp;&nbsp;
                </c:forEach>
    
    
 
                <a href="${path}/board/view.do?idx=${row.idx}">
                ${row.subject}</a>
                <c:if test="${row.comment_count >0 }">
            
            <span style="color:red">[${row.comment_count}]</span>
                
                </c:if>
                </td>
    
                <td>${row.post_date}</td>
                <td>${row.hit}</td>
                
                
                <!-- 첨부파일의 사이즈가 보다 크면 파일 다운로드 링크 표시 -->
                
                
                <td align="center">
                <c:if test="${row.filesize > 0 }">
                    <a href="${path}/board/down.do?idx=${row.idx}">
                    <img src="${path}/images/file.gif">
                </a>
                </c:if>
                </td>
 
                <td>${row.down}</td>
            </tr>
        </c:forEach>
 
 
<!-- page navigation -->
<tr align="center">
<td colspan="7">
<!-- 이전 -->
<c:if test="${page.curPage>1 }">
<a href="${path}/board/board_list.do?curPage=1&search_option=${search_option}&search=${search}">[시작] </a>
</c:if>
 
<c:if test="${page.curBlock > 1 }">
<a href="${path}/board/board_list.do?curPage=${page.prevPage}&search_option=${search_option}&search=${search}">[이전] </a>
</c:if>
 
 
 
<c:forEach var="pageNum" begin="${page.blockStart}" end="${page.blockEnd}">
 
<c:choose>
    <c:when test="${pageNum==page.curPage }">
 
    <span style="color:red">[${pageNum}]</span>
    </c:when>
    
    <c:otherwise>
<a href="${path}/board/board_list.do?curPage=${pageNum}&search_option=${search_option}&search=${search}">[${pageNum}] </a>&nbsp;
    </c:otherwise>
</c:choose>
 
</c:forEach>
 
<!-- 다음 -->
 
 
<c:if test="${page.curBlock <= page.totBlock}">
 
<a href="${path}/board/board_list.do?curPage=${page.nextPage}&search_option=${search_option}&search=${search}">[다음] </a>
</c:if>
 
<!-- 마지막 페이지 -->
 
<c:if test="${page.curPage < page.totPage}">
 
<a href="${path}/board/board_list.do?curPage=${page.totPage}&search_option=${search_option}&search=${search}">[끝] </a>
 
</c:if>
</td>
 
</tr>
 
    </table>
 
<br>
<!-- 검색폼 -->
 
    <form method="post" action="${path}/board/board_list.do">
        <div align="center"><select name="search_option">
            <option value="username"
            
            <c:if test= "${search_option =='username'}"> selected </c:if> >
            이름
            </option>
            
            <option value="subject" <c:if test= "${search_option =='subject'}"> selected </c:if> >제목</option>
            <option value="content" <c:if test= "${search_option =='content'}"> selected </c:if> >내용</option>
            <option value="all" <c:if test= "${search_option =='all'}"> selected </c:if> >전체</option>
        </select>
        
        <input type="text" name="search" value="${search}">
        <input type="submit" value="확인">&nbsp;&nbsp;&nbsp;
        <input type="button" id="btnWrite" value="글쓰기"></div>
    </form>    
 
    </article>
          <jsp:include page="boardSidebar.jsp" flush="false"/>
 
          <jsp:include page="boardFooter.jsp" flush="false"/>
 
cs





controller 



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
 
 
@RequestMapping("board/board_list.do")
    public String board_list(@RequestParam(defaultValue = "1"int curPage, Model model,
            @RequestParam(required = false, defaultValue = "username"String search_option,
            @RequestParam(required = false, defaultValue = ""String search) {
 
        /*
         * @RequestParam에 지정된 변수에 값이 넘어오지 않으면 400에러 발생 required=false 필수 입력항목이
         * 아님, 기본값은 required=true defaultValue= "기본값"
         */
 
        int count = boardService.boardCount(search_option, search);
 
        PageDAO pageDao = new PageDAO(count, curPage);
 
        int start = pageDao.getPageBegin();
        int end = pageDao.getPageEnd();
 
        List<BoardDTO> list = boardService.boardList(start, end, search_option, search);
 
        model.addAttribute("list", list);
 
        // 페이지 네비게이션 관련자료 저장
        model.addAttribute("page", pageDao);
 
        // 검색관련 정보 저장
        model.addAttribute("search_option", search_option);
        model.addAttribute("search", search);
 
        Logger.info("�럹�씠吏��젙蹂�:" + pageDao);
 
        return "board/board_list";
 
    }
    
    
cs




3.page dao


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.example.web03.model.board.dao;
 
public class PageDAO {
 
    public static final int PAGE_SCALE = 10// 페이지당 게시물 수
    public static final int BLOCK_SCALE = 5// 블록당 페이지수
 
    private int curPage; // 현재 페이지 번호
    private int prevPage; // 이전 페이지
    private int nextPage; // 다음 페이지
    private int totPage; // 전체 페이지 갯수
    private int curBlock; // 현재 페이지 블록 번호
    private int totBlock; // 전체 페이지 블록 갯수
    private int pageBegin; // 페이지 내에서의 레코드 시작 번호
    private int pageEnd; // 페이지 내에서의 레코드 마지막 번호
    private int blockStart; // 페이지 블록 내에서의 시작 페이지 번호
    private int blockEnd; // 페이지 블록 내에서의 마지막 페이지 번호
 
    public int getCurPage() {
        return curPage;
    }
 
    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }
 
    public int getPrevPage() {
        return prevPage;
    }
 
    public void setPrevPage(int prevPage) {
        this.prevPage = prevPage;
    }
 
    public int getNextPage() {
        return nextPage;
    }
 
    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }
 
    public int getTotPage() {
        return totPage;
    }
 
    public void setTotPage(int count) {
 
        totPage = (int) Math.ceil(count * 1./ PAGE_SCALE);
 
    }
 
    public int getCurBlock() {
        return curBlock;
    }
 
    public void setCurBlock(int curBlock) {
        this.curBlock = curBlock;
    }
 
    public int getTotBlock() {
        return totBlock;
    }
 
    public void setTotBlock(int totBlock) {
        this.totBlock = totBlock;
    }
 
    public int getPageBegin() {
        return pageBegin;
    }
 
    public void setPageBegin(int pageBegin) {
        this.pageBegin = pageBegin;
    }
 
    public int getPageEnd() {
        return pageEnd;
    }
 
    public void setPageEnd(int pageEnd) {
        this.pageEnd = pageEnd;
    }
 
    public int getBlockStart() {
        return blockStart;
    }
 
    public void setBlockStart(int blockStart) {
        this.blockStart = blockStart;
    }
 
    public int getBlockEnd() {
        return blockEnd;
    }
 
    public void setBlockEnd(int blockEnd) {
        this.blockEnd = blockEnd;
    }
 
    // 생성자
    public PageDAO(int count, int curPage) {
 
        curBlock = 1// 현재 페이지 블록을 1로 설정
        this.curPage = curPage; // 현재 페이지 번호 설정
        setTotPage(count); // 전체 페이저 갯수 설정
        setPageRange(); // 편재 페이지 시작번호, 끝번호 계산
        setTotBlock(); // 전체 페이지 블록 갯수 계산
        setBlockRange(); // 현재 페이지 블록의 시작 페이지 끝페이지 번호 계산
 
    }
 
    // 현재 페이지가 몇번째 페이지에 속하는지 계산
    public void setBlockRange() {
 
        
        //현재 페이지가 몇번째 페이지 블록에 속하는지 계산
        curBlock = (int) Math.ceil((curPage - 1/ BLOCK_SCALE) + 1
        blockStart = (curBlock - 1* BLOCK_SCALE + 1;    //시작번호
 
        blockEnd = blockStart + BLOCK_SCALE - 1;        //끝번호
 
        if (blockEnd > totPage) {        //마지막 페이지가 범위를 초과할 경우
            blockEnd = totPage;
        }
        
        //현재 블록이 1이면 이전 페이지를 1로 설정
        prevPage = curBlock == : (curBlock - 1* BLOCK_SCALE;
 
        //현재 블록이 마지막 블록이면 다음 페이지를 마지막 페이지 번호로 설정
        nextPage = curBlock > totBlock ? (curBlock * BLOCK_SCALE) : (curBlock * BLOCK_SCALE) + 1;
 
        //마지막 페이지가 범위를 넘지 않도록 처리
        if (nextPage >= totPage) {
 
            nextPage = totPage;
        }
    }
 
    // 전체 페이지 블록 갯수 계산
    public void setTotBlock() {
 
        totBlock = (int) Math.ceil(totPage / BLOCK_SCALE);
 
    }
 
    // 현제페이지의 시작번호, 끝번호 계산
    public void setPageRange() {
 
        pageBegin = (curPage - 1* PAGE_SCALE + 1;
        pageEnd = pageBegin + PAGE_SCALE - 1;
 
    }
 
}
 
 
cs




4service


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
    
    @Service 
public class BoardServiceImpl implements BoardService {
 
    @Inject
    BoardDAO boardDAO; // BoardDAOImpl �씤�뒪�꽩�뒪瑜� �깮�꽦�븯�뿬 二쇱엯�떆�궡
 
    @Inject
    CommentDAO commentDao;
    
    @Override
    public List<BoardDTO> boardList(int start, int end,String search_option, String search) {
 
        List<BoardDTO> list;
 
        list = boardDAO.boardList(start, end,search_option,search);
 
        return list;
    }
    
    
    
        //count
    @Override
    public int boardCount(String search_option, String search) {
 
        return boardDAO.boardCount(search_option,search);
    }
 
    
cs



5dao


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
//dao 
    
    
        public List<BoardDTO> boardList(int start, int end, String search_option, String search) {
 
        List<BoardDTO> list = null;
 
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("start", start);
            map.put("end", end);
            map.put("search_option", search_option);
            map.put("search", search);
            
            list = sqlSession.selectList("boardList", map);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return list;
    }
    
    
    
        @Override
    public int boardCount(String search_option, String search) {
 
        int result = 0;
 
        try {
            
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("search_option", search_option);
            map.put("search", search);
            
            result = sqlSession.selectOne("boardCount",map);
        } catch (Exception e) {
                e.printStackTrace();
        }
 
        return result;
    }
    
    
cs



mapper 


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//mapper
    
    
    <select id="boardList" resultType="boardDto">
        select
        idx,userid,username,subject,hit,post_date,filename,filesize,down,comment_count,ref,
        depth, reorder
        from
        (
        select A.*,rownum as rn
        from
        (
        select b.IDX, m.USERID,
        m.USERNAME,
        b.SUBJECT, b.hit, b.POST_DATE, b.filesize, b.filename,
        b.down,
        (select
        count(*) from board_comment where board_idx=b.idx)
        comment_count
        ,ref, depth, reorder
        from
        board b,
        TBL_MEMBER m
        where
        b.USERID = m.USERID
        <if test="search_option !='all' ">
            and
            ${search_option} like '%'||#{search}||'%'
 
        </if>
 
        <if test="search_option=='all' ">
 
            and
            (username like '%'||#{search}||'%'
 
            or subject like '%' || #{search} ||'%'
            or content like '%' || #{search}
            ||'%'
            )
 
        </if>
        order by ref desc, reorder asc )
        A
        )
        where rn
        between
        #{start} and #{end}
 
 
    </select>
 
    
    
    
    <select id="boardCount" resultType="int">
 
        select count(*)
        from board b, TBL_MEMBER m
        where b.userid=m.userid
 
        <if test="search_option !='all' ">
            and
            ${search_option} like '%'||#{search}||'%'
 
        </if>
 
        <if test="search_option=='all' ">
 
            and
            (username like '%'||#{search}||'%'
 
            or subject like '%' || #{search} ||'%'
            or content like '%' || #{search}
            ||'%'
            )
 
        </if>
 
 
    </select>
    
    
    
 -----------------------------------------------------   
    
    
-- mysql procduer insert 
 
DELIMITER $$
DROP PROCEDURE IF EXISTS FILL_RATE_TEST_DATA$$
CREATE PROCEDURE FILL_RATE_TEST_DATA()
BEGIN
    DECLARE i INT DEFAULT 1;
    
 
    WHILE i <= 567 DO
        
insert into board(userid,subject,content,ref) values
('kim2','제목','내용',(select * from (select ifnull ( max(idx)+1) from board) as idx));
        
        SET i = i + 1;
    END WHILE;
END$$
DELIMITER $$
 
 
-- call proceduer
CALL FILL_RATE_TEST_DATA();
 
 
select userid from member;
select userid from board;
 
 
--  board , member join 
 
select idx, b.userid,name,subject,hit,post_date
from board b,member m
where b.userid=m.userid 
order by idx limit 0,10;
 
-- board count
select count(*) from board;
    
    
cs


반응형

'매일코딩 > Spring' 카테고리의 다른 글

28.spring - hitup  (4) 2016.11.18
27.springproject - log  (4) 2016.11.18
25.스프링프로젝트 - aop  (6) 2016.11.17
24.스프링프로젝트 - download  (0) 2016.11.17
23.스프링프로젝트 - board write  (0) 2016.11.17

댓글