话不多说..直接贴代码...
下载地址:http://download.youkuaiyun.com/detail/zouhao619/4553604
PagerUtil.java:
package com.zpass.util;
import org.apache.struts2.ServletActionContext;
import com.zpass.action.CommonAction;
/*
* 分页类 亲自手写
*/
public class PagerUtil {
private long Start=0;//起始数
private long End;//结束数
private int ListPage=20;//每页显示数
private String Parameter="p";//页数跳转时要带的参数
private long Count;//总数
private int CurrentPage=1;//当前页
private int TotalPages;//总页数
private int ListNumber=5;//前后显示多少
private String Url;
public long getStart() {
return Start;
}
public int getListPage() {
return ListPage;
}
private void CalculatePage(int currentPage)
{
if(currentPage<=1)
this.CurrentPage=1;
else
this.CurrentPage=currentPage;
this.Start=(this.CurrentPage-1)*this.ListPage;
this.End=this.Start+this.ListPage;
}
private void SetTotalPages()
{
if(this.Count%this.ListPage==0)
this.TotalPages=(int) (this.Count/this.ListPage);
else
this.TotalPages=(int) (this.Count/this.ListPage+1);
}
//保持其他参数同步
private void GetParameter()
{
Url=ServletActionContext.getRequest().getQueryString();
//如果没有参数
if(Url==null){
Url=ServletActionContext.getRequest().getRequestURI()+"?";//获取当前页面地址
}else{
//如果参数(不只p,还有其他参数)
if(Url.indexOf("p=")>0){
Url=Url.substring(0,Url.indexOf("&p="));
Url=ServletActionContext.getRequest().getRequestURI()+"?"+Url+"&";
}else{
Url=ServletActionContext.getRequest().getRequestURI()+"?";//获取当前页面地址
}
}
}
//开始分页
public String ShowPager(long count)
{
this.GetParameter();
this.Count=count;
if(ServletActionContext.getRequest().getParameter(this.Parameter)!=null){
this.CalculatePage(Integer.parseInt(ServletActionContext.getRequest().getParameter(this.Parameter)));
}else{
this.CalculatePage(1);
}
this.SetTotalPages();
String Page=this.Count+" 条记录 ";
Page=Page+this.CurrentPage+"/"+this.TotalPages+" 页";
String a_class="class='number'";
String current="class='number current'";
if(this.CurrentPage-this.ListNumber>0){
Page=Page+" <a href='"+Url+this.Parameter+"=1' "+current+">第一页</a>";
Page=Page+" <a href='"+Url+this.Parameter+"="+(int)(this.CurrentPage-this.ListNumber)+"' "+current+">前"+this.ListNumber+"页</a>";
}
if(this.CurrentPage>1)
Page=Page+" <a href='"+Url+this.Parameter+"="+(int)(this.CurrentPage-1)+"' "+current+">上一页</a>";
if(this.CurrentPage<this.TotalPages)
Page=Page+" <a href='"+Url+this.Parameter+"="+(int)(this.CurrentPage+1)+"' "+current+">下一页</a>";
if(this.CurrentPage>1){
String PageVo="";
for(int i=this.CurrentPage-1;i>this.CurrentPage-this.ListNumber;i--){
if(i<1)
break;
PageVo="<a href='"+Url+this.Parameter+"="+i+"' "+a_class+">"+i+"</a>"+PageVo;
}
Page=Page+PageVo;
}
if(this.CurrentPage<=this.TotalPages && this.TotalPages!=1){
for(int i=this.CurrentPage;i<this.CurrentPage+this.ListNumber;i++){
if(i==this.CurrentPage){
Page=Page+"<a href='#' "+current+">"+i+"</a>";
continue;
}
if(i>this.TotalPages)
break;
Page=Page+"<a href='"+Url+this.Parameter+"="+i+"' "+a_class+">"+i+"</a>";
}
}
if(this.CurrentPage+this.ListNumber<this.TotalPages)
{
Page=Page+" <a href='"+Url+this.Parameter+"="+(int)(this.CurrentPage+this.ListNumber)+"' "+current+">后"+this.ListNumber+"页</a>";
Page=Page+" <a href='"+Url+this.Parameter+"="+this.TotalPages+"' "+current+">最后一页</a>";
}
return Page;
}
}
有了这个分页类...
我们在action就这么调用就可以了
PagerUtil pager=new PagerUtil();
//这里是计算表中的总数(我自己写的dao层,请对号入座,这里直接复制没用)
int count=userService.getCommonDao().table("User").Count();String page=pager.ShowPager(count);
//这里是获取返回的List(我自己写的service层,请对号入座,这里直接复制没用)
List<User> list=this.userService.getCommonDao().table("User").order("id desc").limit((int)pager.getStart(),CommonAction.config.getPAGE_LISTPAGE()).select();
//这里是我将list和page返回给Jsp,你也可以不这样做
Map<String,Object> request = (Map)ActionContext.getContext().get("request");
request.put("page",page);
request.put("list",list);
那么在Jsp层显示的话就是
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<s:iterator value="#request.list" id="vo">
${vo.username }
</s:iterator>
${request.page}
</body>
</html>