liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
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
package framework.mybatis.pageAction;
 
import javax.servlet.http.HttpServletRequest;
 
public class PageInfo {
    public final static String REQUEST_ATTRIBUTE_PAGE_INFO = "REQUEST_ATTRIBUTE_PAGE_INFO";
    
    public final static int DEFAULT_COUNT_PER_PAGE = 10;
    
    private int page = 1;
    private int countPerPage = DEFAULT_COUNT_PER_PAGE;
    private int totalCount;
    private int startRow;
    private int endRow;
    private int totalPage;
    
    protected HttpServletRequest request;
    
    protected PageInfo(){
        
    }
    /**
     * 设置总条数,计算分页参数
     * 
     * @param totalCount
     * @param countPerPage
     * @param page
     */
    public void setTotalCount(int totalCount){
        this.totalCount = totalCount;
        
        if (this.totalCount % this.countPerPage == 0) {
            totalPage = (int) (this.totalCount / this.countPerPage);
        } else {
            totalPage = (int) (this.totalCount / this.countPerPage) + 1;
        }
        if(totalPage==0){
            totalPage = 1;
        }
        if(this.page<1){
            this.page=1;
        }
        if(this.page>totalPage){
            this.page=totalPage;
        }
        if (this.totalCount == 0) {
            startRow=0;
            endRow=0;
        }else{
            startRow=this.countPerPage*(this.page-1)+1;
            endRow = this.countPerPage * this.page;
            endRow = endRow > this.totalCount ? this.totalCount : endRow;
        }
 
        //save to request
        request.setAttribute(REQUEST_ATTRIBUTE_PAGE_INFO, this);
        request.setAttribute("pageSearchStartRow", Integer.valueOf(startRow));
 
    }
    
    /**
     * 得到总页数
     * @return
     */
    public int getTotalPage(){
        return totalPage;
    }
    /**
     * 得到当前页数
     * @return
     */
    public int getPage() {
        return page;
    }
    /**
     * 得到每页多少条
     * @return
     */
    public int getCountPerPage() {
        return countPerPage;
    }
    /**
     * 得到总条数
     * @return
     */
    public int getTotalCount() {
        return totalCount;
    }
    /**
     * 得到开始的行数,从第1行开始,如果没有,则为0
     * @return
     */
    public int getStartRow() {
        return startRow;
    }
    /**
     * 得到结束行数,从第1行开始,如果没有,则为0
     * @return
     */
    public int getEndRow() {
        return endRow;
    }
 
    /**
     * 设置当前页数
     * @param page
     */
    public void setPage(int page) {
        if(page <1){
            page = 1;
        }
        this.page = page;
    }
    /**
     * 设置每页条数
     * @param countPerPage
     */
    public void setCountPerPage(int countPerPage) {
        if(countPerPage<1){
            countPerPage = 1;
        }
        this.countPerPage = countPerPage;
    }
 
}