Webwork+hibernate+spring分页实现
Webwork+Spring+Hibernate中的分页方法
在实际的开发中,学习了其他人的分页方案,经过修改形成了一个可以直接使用的分页方案,现说明如下。 该分页可以实现定制每页显示的记录数、上下翻页和跳到一个指定的页面的功能。
一、定义了一个Page类和PageHelper类。
Page.java
public class Page {
//-----------------数据库相关参数-----------------
//默认每页显示记录数?
private int perPage = 10;
//页码
private int page = 1;
//记录总数
private int total = 0;
//开始记录数(新的游标起始位置)
private int startRs=0;
//翻页的url(路径),不带参数,参数由其它属性指定
private String url = "";
//------------------页面相关参数------------------
//首页
private String firstPage = "";
//上一页
private String prePage = "";
//下一页
private String nextPage = "";
//最后一页
private String lastPage = "";
//每页显示记录数
private String numPage = "";
//跳转到第几页
private String jumpPage = "";
//总页数
private int totalPage = 1;
//
private String hql = "";
public Page() {
}
//省略getXXX()和setXXX()
PageHelper.java
public class PageHelper {
private boolean isHaveParam = false;//是否有参数?
public PageHelper() {
//
}
/**
* 用于计算用户自定义每页显示记录数
*
* @return
*/
public int doCustomPerPage(int customPerPage) {
int perPage = customPerPage;
String cPerPage = String.valueOf(perPage);
if (!"".equals(cPerPage)) {
ActionContext.getContext().getSession().put(Constant.COUNT_PERPAGE,cPerPage);
}
String cUserPerPage =
(String) ActionContext.getContext().getSession().get(Constant.COUNT_PERPAGE);
if (null != cUserPerPage) {
perPage = Integer.parseInt(cUserPerPage);
}
return perPage;
}
public Page getNextPageObject(Page pageObject) {
int page = 1;
int total = 0;
int perPage = 20;
// 取分页器参数
//页码
page = pageObject.getPage();
//总记录数
total = pageObject.getTotal();
//每页显示的记录数
perPage = pageObject.getPerPage();
//需要分页的action名称
String url = pageObject.getUrl();
boolean display1 = false;//有首页?
boolean display2 = false;//有上一页?
boolean display3 = false;//有下一页?
boolean display4 = false;//有最后一页?
//总页数
int totalPage = (total + perPage - 1) / perPage;
if (page > totalPage || page < 0) {
page = 1;
}
// 首页
StringBuffer firstPageBuffer = new StringBuffer();
if (totalPage > 1) {
if(isHaveParam){
firstPageBuffer.append(url + "&pageNo=1");
}else{
firstPageBuffer.append(url + "?pageNo=1");
}
display1 = true;
}
// 上一页?
StringBuffer prePageBuffer = new StringBuffer();
if (page > 1) {
if(isHaveParam){
prePageBuffer.append(url + "&pageNo=" + (page - 1));
}else{
prePageBuffer.append(url + "?pageNo=" + (page - 1));
}
display2 = true;
}
// 下一页?
StringBuffer nextPageBuffer = new StringBuffer();
if (page < totalPage) {
if(isHaveParam){
nextPageBuffer.append(url + "&pageNo=" + (page + 1));
}else{
nextPageBuffer.append(url + "?pageNo=" + (page + 1));
}
display3 = true;
}
// 最后一页?
StringBuffer lastPageBuffer = new StringBuffer();
if (totalPage > 1) {
if(isHaveParam){
lastPageBuffer.append(url + "&pageNo=" + totalPage);
}else{
lastPageBuffer.append(url + "?pageNo=" + totalPage);
}
display4 = true;
}
// 每页显示记录数?
StringBuffer numPageBuffer = new StringBuffer();
numPageBuffer.append("<select name=/"perPage/" ");
numPageBuffer.append(" οnchange=/"");
numPageBuffer.append(" if(this.options[this.selectedIndex]");
numPageBuffer.append(".value!=''){");
if(isHaveParam){
numPageBuffer.append(" location='" + url + "&");
}else{
numPageBuffer.append(" location='" + url + "?");
}
numPageBuffer.append("pageNo=1");
numPageBuffer.append("&perPageCount='+this.options[this.selectedIndex]");
numPageBuffer.append(".value;}/">");
for (int i = 1; i <= 4; i++) {
numPageBuffer.append("<option value=/"" + (i * 10) + "/"");
if ((i * 10) == perPage) {
numPageBuffer.append(" selected>" + (i * 10) + "</option>");
} else {
numPageBuffer.append(">" + (i * 10) + "</option>");
}
}
numPageBuffer.append("</select>");
pageObject.setNumPage(numPageBuffer.toString());
// 跳转到第几页
StringBuffer jumpPageBuffer = new StringBuffer();
jumpPageBuffer.append("<select name=/"pages/"");
jumpPageBuffer.append(" οnchange=/"");
jumpPageBuffer.append(" if(this.options[this.selectedIndex]");
if(isHaveParam){
jumpPageBuffer.append(".value!=''){location='" + url + "&");
}else{
jumpPageBuffer.append(".value!=''){location='" + url + "?");
}
jumpPageBuffer.append("pageNo='+this.options[this.selectedIndex]");
jumpPageBuffer.append(".value;}/">");
for (int i = 1; i <= totalPage; i++) {
jumpPageBuffer.append("<option value=/"" + i + "/"");
if (i == page) {
jumpPageBuffer.append("selected");
}
jumpPageBuffer.append(">" + i + "</option>");
}
jumpPageBuffer.append("</select>");
pageObject.setJumpPage(jumpPageBuffer.toString());
pageObject.setTotal(total);
pageObject.setPage(page);
pageObject.setTotalPage(totalPage);
if (display1) {
pageObject.setFirstPage("<A HREF=/""+firstPageBuffer.toString()+"/">首 页</A>");
}else{
pageObject.setFirstPage("首 页");
}
if (display2) {
pageObject.setPrePage("<A HREF=/""+prePageBuffer.toString()+"/">上一页</A>");
}else{
pageObject.setPrePage("上一页");
}
if (display3) {
pageObject.setNextPage("<A HREF=/""+nextPageBuffer.toString()+"/">下一页</A>");
}else{
pageObject.setNextPage("下一页");
}
if (display4) {
pageObject.setLastPage("<A HREF=/""+lastPageBuffer.toString()+"/">尾 页</A>");
}else{
pageObject.setLastPage("尾 页");
}
pageObject.setNumPage(numPageBuffer.toString());
pageObject.setJumpPage(jumpPageBuffer.toString());
return pageObject;
}
/**
* @return Returns the isHaveParam.
*/
public boolean isHaveParam() {
return isHaveParam;
}
/**
* @param isHaveParam The isHaveParam to set.
*/
public void setHaveParam(boolean isHaveParam) {
this.isHaveParam = isHaveParam;
}
}
Page类主要是存储分页时每页的相关信息,PageHelper类主要是根据用户通过URL送来的参数设置Page对象的相关属性,然后再用这些属性设置页面中的分页。
二、页面中的代码
<!--
<table width="20%" align="center"><tr><td height="5"></td></tr></table>
<table width="75%" align=center cellpadding="0" cellspacing="0" bgcolor="#F4F4F4" style="border: 1 solid #CCCCCC">
<thead>
<tr>
<td height="30"> 共 <font color=blue><ww:property value="page.total"/></font> 条记录
<ww:property value="page.firstPage"/>
<ww:property value="page.prePage"/>
<ww:property value="page.nextPage"/>
<ww:property value="page.lastPage"/>
页次:<ww:property value="page.page"/>/<ww:property value="page.totalPage"/>
每页显示 <ww:property value="page.numPage"/> 条
跳转到第 <ww:property value="page.jumpPage"/> 页</td>
</tr>
</thead>
</table>
-->
主要是把page对象的属性通过Webwork中定义的标签显示在页面中。
三、Action中的调用代码
public String execute() throws Exception{
if (logger.isDebugEnabled()) {
logger.debug("teamId="+teamId+"--载入日志成功!");
}
user = (User)ActionContext.getContext().getSession().get(Constant.LOGIN_USER_KEY);
//如果是用户,设定组织编号。如果是领导,则通过参数给组织编号赋值。
if( (null == teamId) || ("".equals(teamId))){
this.setTeamId(user.getOrgan().getId());
}
int perPage = 10;
String totalHql =
"select count(*) from Log as log where log.user.organ.id='"+teamId+"'";
PageHelper pageHelper = new PageHelper();
if(perPageCount != -1){
perPage = pageHelper.doCustomPerPage(perPageCount);
}
Page pageObj = new Page();
pageObj.setPerPage(perPage);
pageObj.setPage(pageNo);
pageObj.setHql(" from Log log order by log.id desc where log.user.organ.id='"+teamId+"'");
pageObj.setTotal(this.logDao.getTotal(totalHql));
if(user.isLeader()){
this.setTeams(logDao.listTeams());
pageHelper.setHaveParam(true);
pageObj.setUrl("listlogs"+Constant.FILE_SUFFIX+"?teamId="+teamId);
}else{
pageObj.setUrl("listlogs"+Constant.FILE_SUFFIX);
}
pageObj.setStartRs((pageNo-1) * perPage);
page = pageHelper.getNextPageObject(pageObj);
//列出组织
this.setLogs(this.logDao.getList(page));
//根据组织ID获得名称
this.setTeamName(logDao.getNameById(teamId));
if (logger.isDebugEnabled()) {
logger.debug("载入session用户成功!");
}
return SUCCESS;
}