一个分页的小算法,分页中间有数字的那种

public class PageNumber {
public static String GetPage(String url, int pageindex, int pagesize,
int pagecount, int pagemaxvalue) {
int _pagenumber = 0;
int pagemin = 0;
if (pagesize <= 0)
pagesize = 1;

_pagenumber = (pagecount + pagesize + 1) / pagesize;

if (pagesize > _pagenumber)
pagesize = _pagenumber;
if (pageindex <= 0)
pageindex = 1;

int min = (_pagenumber - pagemaxvalue) > 0 ? _pagenumber - pagemaxvalue
+ 1 : 1;
int space = pagemaxvalue / 2;
pagemin = (pageindex < min) ? pageindex : min;
if (pagemin > 1) {
for (int _min = 1; _min <= space; _min++) {
if (pagemin > 1)
pagemin = (pageindex - _min) < pagemin ? pageindex - _min
: pagemin;
else
break;
}
}
if (_pagenumber <= 1)
return "";

String output = "";
output += "<div class=\"page\">";
int pagemax = (pagemin + pagemaxvalue) <= _pagenumber ? pagemin+ pagemaxvalue : _pagenumber + 1;

output += String.format("<span><a href=\"%s\">第一页</a></span>", String.format(url, 1));
output += String.format("<span><a href=\"%s\">上一页</a></span>", String.format(url, (pageindex - 1) > 0 ? (pageindex - 1) : 1));
for (int i = pagemin; i < pagemax; i++) {
output += String.format("<span><a href=\"%s\"%s>" + i
+ "</a></span>", String.format(url, i),
(pageindex == i) ? "class=\"current\"" : "");
}
output += String.format("<span><a href=\"%s\">下一页</a></span>", String
.format(url, ((pageindex + 1) < _pagenumber) ? pageindex + 1
: _pagenumber));
output += String.format("<span><a href=\"%s\">最后一页</a></span>", String
.format(url, _pagenumber));
output += "</div>";
return output;
}
}

点赞