Jsp中翻页功能实现
第一步:新建两个辅助类
Page类
public class Page {
// 1.每页显示数量(everyPage)
private int everyPage;
// 2.总记录数(totalCount)
private int totalCount;
// 3.总页数(totalPage)
private int totalPage;
// 4.当前页(currentPage)
private int currentPage;
// 5.起始点(beginIndex)
private int beginIndex;
// 6.是否有上一页(hasPrePage)
private boolean hasPrePage;
// 7.是否有下一页(hasNextPage)
private boolean hasNextPage;
public Page(int everyPage, int totalCount, int totalPage, int currentPage,
int beginIndex, boolean hasPrePage, boolean hasNextPage) {
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
//构造函数,默认
public Page(){}
//构造方法,对所有属性进行设置
//*****此处省略set及get构造方法****
}
PageUtil类
package com.iven.help;
/*
* 分页信息辅助类
*/
public class PageUtil {
public static Page createPage(int everyPage,int totalCount,int currentPage) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
public static Page createPage(Page page,int totalCount) {
int everyPage = getEveryPage(page.getEveryPage());
int currentPage = getCurrentPage(page.getCurrentPage());
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
//设置每页显示记录数
public static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
//设置当前页
public static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
//设置总页数,需要总记录数,每页显示多少
public static int getTotalPage(int everyPage,int totalCount) {
int totalPage = 0;
if(totalCount % everyPage == 0) {
totalPage = totalCount / everyPage;
} else {
totalPage = totalCount / everyPage + 1;
}
return totalPage;
}
//设置起始点,需要每页显示多少,当前页
public static int getBeginIndex(int everyPage,int currentPage) {
return (currentPage - 1) * everyPage;
}
//设置是否有上一页,需要当前页
public static boolean getHasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
//设置是否有下一个,需要总页数和当前页
public static boolean getHasNextPage(int totalPage, int currentPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
第二步:在Action类中创建Page对象,并存入到Session中
public String getUserList(){
//从Session中获取用户
User user=(User)ActionContext.getContext().getSession().get("user");
System.out.println(user.getId());
//从request对象中获取currentPage的值
StringcurPage=ServletActionContext.getRequest().getParameter("currentPage").toString()
int currentPage=Integer.parseInt(curPage);
//根据uid从Server层中获取User表中的总数
int totalCount=userDao.getCountByUid(user.getId());
//通过PageUtil的createPage静态方法创建page对象
Page page=PageUtil.createPage(3, totalCount, currentPage);
//根据page及uid获取users列表
List<User> users=userDao.findUserByPage(page,user.getId());
//将users及page存入到Session中
ActionContext.getContext().getSession().put("users",users);
ActionContext.getContext().getSession().put("page", page);
return SUCCESS;
}
第三步:在Dao层中中获取数据对象
获取表中记录总数,
public ClassuserDao{
public int getCountByUid(int uid){
List<User> users=null;
Query query=getSession().createQuery("from Userwhere uid="+uid);
users=query.list();
return users.size();
}
根据page及uid获取user列表
public List<User> findUserByPage(Page page,int uid) {
Query query=getSession().createQuery("from User");
query.setMaxResults(page.getEveryPage());
query.setFirstResult(page.getCurrentPage());
List<User> users=query.list();
return repairs;
}
}
第四步:在Jsp页面中使用page对象实现翻页
<tr >
<td colspan="9" align="center" >
共${page.totalCount }条记录,当前第${page.currentPage }/${page.totalPage }页,每页${page.everyPage }条记录
<a href="getUserList.action?currentPage=1" >首页</a>|
<a href="getUserList.action?currentPage=${page.currentPage -1}" >上一页</a>|
<a href="getUserList.action?currentPage=${page.currentPage +1}" >下一页</a>|
<a href="getUserList.action?currentPage=${page.totalPage }" >尾页</a>
</td>
</tr>
第五步:测试
<a href="getUserList.action?currentPage=1" >