package org.uscp.vo;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Pagination<T> {
public static final int DEFAULT_PAGE_SIZE = 10;
public static final String PAGINATION = "pagination";
class Config{
/**
* 指定页面跳转时接收跳转页数的param变量
*/
public String page = "page";
/**
* 指定跳转的路径
*/
public String path = "";
/**
* 指定分页时的查询参数
*/
public Map<String,String> queryParams = new HashMap<String,String>();
}
/**
* 分页的配置
*/
private Config config = new Config();
/**
* 使用默认的构造函数后必须调用 reBuild(long,long)方法
*/
public Pagination(){
}
public void reBuild(long currentPage,long total,int pageSize){
this.currentPage = currentPage;
this.total = total;
this.lastPage = total % pageSize == 0 ? total/pageSize : (total/pageSize + 1);
this.hasMore = this.currentPage < this.lastPage;
}
/**
* 自定义配置的构造函数
* @param currentPage
* @param total
*/
public Pagination(long currentPage,long total){
reBuild(currentPage,total,this.pageSize);
}
public Pagination(long currentPage,long total,Map<String,String> queryParams){
this(currentPage, total);
if(null != queryParams){
config.queryParams = queryParams;
}
}
/**
* 自定义配置的构造函数
* @param page
* @param path
* @param queryParams
*/
public Pagination(long currentPage,long total,String page,String path,Map<String,String> queryParams){
this(currentPage, total,queryParams);
if(null != page){
config.page = page;
}
if(null != path){
config.path = path;
}
}
private List<T> items;
/**
* 当前页数
*/
private long currentPage;
/**
* 最后一页页码
*/
private long lastPage;
/**
* 总记录数
*/
private long total;
/**
* 默认每页5个
*/
private int pageSize = DEFAULT_PAGE_SIZE;
private boolean hasMore;
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
public int getPageSize() {
return pageSize;
}
public long getCurrentPage(){
return currentPage;
}
public Map<String,String> getQueryParams(){
return config.queryParams;
}
public long getTotal(){
return total;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
//这里根据pageSize重新计算分页个数及是否有下一页
this.lastPage = total % pageSize == 0 ? total/pageSize : (total/pageSize + 1);
this.hasMore = this.currentPage < this.lastPage;
}
/**
* 是否有分页
* @return
*/
private boolean hasPages(){
return !(1 == this.currentPage && !this.hasMore);
}
/**
* 生成一个禁用的按钮
* @param text
* @return
*/
private String getDisabledTextWrapper(String text){
return "<li class='disabled'><span>" + text + "</span></li>";
}
/**
* 生成一个激活的按钮
* @param text
* @return
*/
private String getActivePageWrapper(String text){
return "<li class='active'><span>" + text + "</span></li>";
}
/**
* 生成省略号按钮
* @return
*/
private String getDots(){
return this.getDisabledTextWrapper("...");
}
/**
* 生成一个可点击的按钮
* @param url
* @param page
* @return
*/
private String getAvailablePageWrapper(String url,String page){
return "<li><a href='" +url + "'>" + page + "</a></li>";
}
/**
* 生成普通页码按钮
* @param url
* @param page
* @return
*/
private String getPageLinkWrapper(String url,String page){
if(page.equals(String.valueOf(this.currentPage))){
return this.getActivePageWrapper(page);
}
return this.getAvailablePageWrapper(url, page);
}
/**
* 批量生成页码按钮.
* @param urls
* @return
*/
private String getUrlLinks(Map<Long,String> urls){
StringBuilder builder = new StringBuilder("");
if(null != urls && urls.size() > 0){
for(Long page : urls.keySet()){
builder.append(this.getPageLinkWrapper(urls.get(page), String.valueOf(page)));
}
}
return builder.toString();
}
/**
* 创建一组分页链接
* @param start
* @param end
* @return
* @throws UnsupportedEncodingException
*/
private Map<Long,String> getUrlRange(long start, long end) throws UnsupportedEncodingException{
Map<Long,String> urls = new HashMap<Long,String>();
for(long page = start;page<=end;page++){
urls.put(page, this.getUrl(page));
}
return urls;
}
/**
* 获取页码对应的链接
* @param page
* @return
* @throws UnsupportedEncodingException
*/
private String getUrl(long page) throws UnsupportedEncodingException{
if(page<0){
page = 1;
}
//组合查询参数
StringBuilder urlBuilder = new StringBuilder();
if(null != config.path){
urlBuilder.append(config.path);
}
urlBuilder.append("?page=").append(page);
if(config.queryParams.size() > 0){
for(String param : config.queryParams.keySet()){
urlBuilder.append("&").append(param).append("=").append(URLEncoder.encode(config.queryParams.get(param),"utf-8"));
}
}
return urlBuilder.toString();
}
/**
* 页码按钮
* @return string
* @throws UnsupportedEncodingException
*/
private String getLinks() throws UnsupportedEncodingException{
int size = 3;
int window = size * 2;
Map<Long,String> first = null;
Map<Long,String> slide = null;
Map<Long,String> last = null;
if(this.lastPage < window + 6){
first = this.getUrlRange(1, this.lastPage);
}else if(this.currentPage <= window){
first = this.getUrlRange(1, window+2);
last = this.getUrlRange(this.lastPage -1 , this.lastPage);
}else if(this.currentPage > (this.lastPage - window)){
first = this.getUrlRange(1, 2);
last = this.getUrlRange(this.lastPage -(window+2) , this.lastPage);
}else{
first = this.getUrlRange(1, 2);
slide = this.getUrlRange(this.currentPage - size, this.currentPage + size);
last = this.getUrlRange(this.lastPage -1 , this.lastPage);
}
StringBuilder builder = new StringBuilder();
if(first.size() > 0){
builder.append(this.getUrlLinks(first));
}
if(null != slide && slide.size() > 0){
builder.append(this.getDots());
builder.append(this.getUrlLinks(slide));
}
if(null != last && last.size()>0){
builder.append(this.getDots());
builder.append(this.getUrlLinks(last));
}
return builder.toString();
}
/**
* 上一页按钮
* @return
* @throws UnsupportedEncodingException
*/
private String getPreviousButton() throws UnsupportedEncodingException{
String character = "«";
if(this.currentPage <= 1){
return this.getDisabledTextWrapper(character);
}
String url = this.getUrl(this.currentPage - 1);
return this.getPageLinkWrapper(url, character);
}
/**
* 下一页按钮
* @return
* @throws UnsupportedEncodingException
*/
private String getNextButton() throws UnsupportedEncodingException{
String character = "»";
if(!this.hasMore){
return this.getDisabledTextWrapper(character);
}
String url=this.getUrl(this.currentPage + 1);
return this.getPageLinkWrapper(url, character);
}
/**
* 渲染分页html
* @return
*/
public String getRender(){
if(this.hasPages()){
StringBuilder builder = new StringBuilder("<ul class='pagination'>");
try{
builder.append(this.getPreviousButton());
builder.append(this.getLinks());
builder.append(this.getNextButton());
}
catch(UnsupportedEncodingException e){
e.printStackTrace();
}
builder.append("</ul>");
return builder.toString();
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的学科竞赛管理系统 (173个子文件)
bootstrap.css 149KB
layui.css 73KB
layer.css 14KB
layui.mobile.css 10KB
laydate.css 7KB
code.css 1KB
59.gif 10KB
22.gif 10KB
24.gif 8KB
13.gif 7KB
16.gif 7KB
39.gif 6KB
64.gif 6KB
63.gif 6KB
50.gif 6KB
loading-0.gif 6KB
4.gif 6KB
1.gif 5KB
42.gif 5KB
71.gif 5KB
21.gif 5KB
20.gif 5KB
29.gif 5KB
70.gif 4KB
5.gif 4KB
17.gif 4KB
27.gif 4KB
9.gif 4KB
44.gif 4KB
11.gif 4KB
8.gif 4KB
3.gif 4KB
23.gif 4KB
34.gif 4KB
41.gif 4KB
38.gif 4KB
65.gif 3KB
32.gif 3KB
45.gif 3KB
7.gif 3KB
12.gif 3KB
26.gif 3KB
60.gif 3KB
2.gif 3KB
40.gif 3KB
25.gif 3KB
19.gif 3KB
66.gif 3KB
18.gif 3KB
46.gif 3KB
10.gif 3KB
28.gif 3KB
51.gif 3KB
57.gif 3KB
67.gif 3KB
0.gif 3KB
48.gif 3KB
43.gif 3KB
30.gif 2KB
61.gif 2KB
33.gif 2KB
69.gif 2KB
14.gif 2KB
47.gif 2KB
36.gif 2KB
49.gif 2KB
58.gif 2KB
6.gif 2KB
54.gif 2KB
53.gif 2KB
56.gif 2KB
62.gif 2KB
31.gif 2KB
55.gif 2KB
35.gif 2KB
15.gif 2KB
loading-2.gif 2KB
37.gif 1KB
68.gif 1KB
52.gif 777B
loading-1.gif 701B
Pagination.java 8KB
DBTemplate.java 6KB
ProjectAction.java 6KB
ProjectCheckAction.java 6KB
AwardAction.java 6KB
ProjectDAO.java 5KB
StringUtil.java 5KB
AwardDAO.java 4KB
CompetitionAction.java 3KB
ProjectService.java 3KB
DaoUtil.java 3KB
ResultSet2BeanUtil.java 2KB
AwardService.java 2KB
CompetitionDAO.java 2KB
Award.java 2KB
Project.java 2KB
StudentDAO.java 2KB
TeamDAO.java 2KB
Competition.java 1KB
共 173 条
- 1
- 2
资源评论
滑头鬼
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功