Page类
PageUtil类
Result类
[color=red]在相应的DAO中添加方法[/color]
[color=red]在service中添加方法[/color]
[color=red]修改Action[/color]
[color=red]在显示页面中添加分页控制栏[/color]
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;
//相应的有参无参构造器,getters和setters方法
}
PageUtil类
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;
}
}
Result类
public class Result {
private Page page;
private List list;
//getter和setter方法
}
[color=red]在相应的DAO中添加方法[/color]
//得到不同页的对象集合
public List findAll(Page page) {
log.debug("finding all record instances");
try {
String queryString = "from record";
Query queryObject = getSession().createQuery(queryString);
queryObject.setFirstResult(page.getBeginIndex());
queryObject.setMaxResults(page.getEveryPage());
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
//得到记录总数
public int queryAllCount() {
log.debug("finding all record counter");
try {
String queryString = "from record";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list().size();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
[color=red]在service中添加方法[/color]
public Result findAllRecord(Page page) {
page = PageUtil.createPage(page,recordDAO.queryAllCount());
List<Record> all = recordDAO.findAll(page);
Result result = new Result();
result.setPage(page);
result.setList(all);
return result;
}
[color=red]修改Action[/color]
public String execute() throws Exception {
//通过调用service来完成查询
Page page = new Page();
page.setCurrentPage(this.getCurrentPage());
//每页显示的记录数
page.setEveryPage(5);
Result result = recordService.findAllRecord(page);
page = result.getPage();
List<Record> records = result.getList();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("records", records);
request.setAttribute("page", page);
return this.SUCCESS;
}
[color=red]在显示页面中添加分页控制栏[/color]
<tr>
<td colspan="11" align="center">
<s:if test="#request.page.hasPrePage">
<a href="FindAll.action?currentPage=1">首页</a>
<a href="FindAll.action?currentPage=
${request.page.currentPage -1 }">上一页</a>
</s:if>
<s:else>
首页
上一页
</s:else>
<s:if test="#request.page.hasNextPage">
<a href="FindAll.action?currentPage=
${request.page.currentPage + 1 }">下一页</a>
<a href="FindAll.action?currentPage=
${request.page.totalPage }">尾页</a>
</s:if>
<s:else>
下一页
尾页
</s:else>
当前第${page.currentPage}页,共${page.totalPage}页,共有${page.totalCount}条记录。
</td>
</tr>