-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaginatorTag.java
137 lines (116 loc) · 3.48 KB
/
PaginatorTag.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
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
package com.dvause.taglib;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.Writer;
public class PaginationTag extends SimpleTagSupport {
private String pageUrl;
private int maxLinks;
private int totalResults;
private int itemsPerPage;
private int currentPage;
private String labelFirst = "First";
private String labelPrev = "Previous";
private String labelNext = "Next";
private String labelLast = "Last";
private boolean showFirst = true;
private boolean showLast = true;
public void doTag() throws JspException {
Writer out = getJspContext().getOut();
int totalPages = (int) Math.ceil((double)totalResults / itemsPerPage);
int pageStart = Math.max(currentPage - maxLinks / 2 ,1);
int pageEnd = pageStart + maxLinks;
if (pageEnd > totalPages + 1) {
int diff = pageEnd - totalPages;
pageStart -= diff - 1;
if (pageStart < 1) {
pageStart = 1;
}
pageEnd = totalPages + 1;
}
try {
out.write("<ul class=\"pagination\">");
if (currentPage > 1) {
if (showFirst) {
out.write(buildPageItem(1, labelFirst, null));
}
out.write(buildPageItem(currentPage - 1, labelPrev, null));
}
if (maxLinks > 0) {
for (int i = pageStart; i < pageEnd; i++) {
if (i == currentPage) {
out.write("<li class=\"active\"><span>" + currentPage + "</span></li>");
} else {
out.write(buildPageItem(i));
}
}
}
boolean lastPage = currentPage == totalPages;
if (!lastPage) {
out.write(buildPageItem((currentPage + 1), labelNext, null));
if (showLast) {
out.write(buildPageItem(totalPages, labelLast, null));
}
}
out.write("</ul>");
} catch (IOException e) {
throw new JspException("Error in WavePaginationTag", e);
}
}
private String buildPageItem(int page) {
return buildPageItem(page, String.valueOf(page), null);
}
private String buildPageItem(int page, String label, String className) {
StringBuilder link = new StringBuilder("<li");
if (className != null) {
link.append( "class=\"").append(className).append("\"");
}
link.append(">")
.append("<a href=\"")
.append(pageUrl);
if (pageUrl.contains("?")) {
link.append("&page=");
} else {
link.append("?page=");
}
link.append(page)
.append("&limit=").append(itemsPerPage)
.append("\">")
.append(label)
.append("</a></li>");
return link.toString();
}
public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}
public void setTotalResults(int totalResults) {
this.totalResults = totalResults;
}
public void setMaxLinks(int maxLinks) {
this.maxLinks = maxLinks;
}
public void setItemsPerPage(int itemsPerPage) {
this.itemsPerPage = itemsPerPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setLabelFirst(String labelFirst) {
this.labelFirst = labelFirst;
}
public void setLabelPrev(String labelPrev) {
this.labelPrev = labelPrev;
}
public void setLabelNext(String labelNext) {
this.labelNext = labelNext;
}
public void setLabelLast(String labelLast) {
this.labelLast = labelLast;
}
public void setShowFirst(boolean showFirst) {
this.showFirst = showFirst;
}
public void setShowLast(boolean showLast) {
this.showLast = showLast;
}
}