pageBean:


package page;


import java.util.List;


public class pageBean {

private List list; // 要返回的某一页的记录列表

private int totalRow; // 总记录数

private int totalPage; // 总页数

private int currentPage; // 当前页

private int pageSize; // 每页记录数


private boolean isFirstPage; // 是否为第一页

private boolean isLastPage; // 是否为最后一页

private boolean hasPreviousPage; // 是否有前一页

private boolean hasNextPage; // 是否有下一页

public List getList() {

return list;

}

public void setList(List list) {

this.list = list;

}

public int getTotalRow() {

return totalRow;

}

public void setTotalRow(int totalRow) {

this.totalRow = totalRow;

}

public int getTotalPage() {

return totalPage;

}

public void setTotalPage(int totalPage) {

this.totalPage = totalPage;

}

public int getCurrentPage() {

return currentPage;

}

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public boolean isFirstPage(){

return currentPage==1;

}

public boolean isLastPage(){

return currentPage==totalPage;

}

public boolean isHasNextPage(){

return currentPage!=totalPage;

}

public boolean isHasPreviousPage(){

return currentPage!=totalPage;

}

public void init() {

isFirstPage = isFirstPage();

isLastPage = isLastPage();

hasNextPage = isHasNextPage();

hasPreviousPage = isHasPreviousPage();

}

public static int countTotalPage(int totalRow,int pageSize){

return ((totalRow%pageSize)==0?(totalRow/pageSize):(totalRow/pageSize+1));

}

public static int countCurrentPage(int page){

System.out.println("scp"+page);

//return ((page==0)?1:page);

if(page==0){

page=1;

System.out.println("scp2"+page);

return page;

}

//int currentPage = (page == 0 ? 1 : page);

System.out.println("scp3"+page);

return page;

}

public static int countOffset(int currentPage,int pageSize){

return (pageSize*(currentPage-1));

}

}




JSP:


共<s:property value="pagebean.totalRow" />条记录

共<s:property value="pagebean.totalPage" />页

当前第<s:property value="pagebean.currentPage" />页


<s:if test="%{pagebean.totalPage==1}">

第一页   最后一页

</s:if>


<s:else>


<s:if test="%{pagebean.isFirstPage()}">第一页

<a href='<s:url action="show.action"><s:param name="currentPage" value="pagebean.currentPage+1"/></s:url>'>下一页</a>

</s:if>


<s:elseif test="%{pagebean.isLastPage()}">最后一页

<a href='<s:url action="show.action"><s:param name="currentPage" value="pagebean.currentPage-1"/></s:url>'>上一页</a>

</s:elseif>


<s:else>

<a href='<s:url action="show.action"><s:param name="currentPage" value="pagebean.currentPage+1"/></s:url>'>下一页</a>

<a href='<s:url action="show.action"><s:param name="currentPage" value="pagebean.currentPage-1"/></s:url>'>上一页</a>

<a href='<s:url action="show.action"><s:param name="currentPage" value="1"/></s:url>'>首页 </a>

<a href='<s:url action="show.action"><s:param name="currentPage" value="pagebean.totalPage"/></s:url>'>最后一页 </a>

</s:else>


</s:else>


DAO:


public List<TUsers> queryForPage(final String hql,final int offset,final int length);


DAOImpl:


public List<TUsers> queryForPage(final String hql, final int offset, final int length) {

// TODO Auto-generated method stub

List tusers=template.executeFind(new HibernateCallback(){


public Object doInHibernate(Session arg0)

throws HibernateException, SQLException {

// TODO Auto-generated method stub

Query query=arg0.createQuery(hql);

System.out.println(offset);

System.out.println(length);

query.setFirstResult(offset);

query.setMaxResults(length);

List tusers=query.list();

return tusers;

}

});

return tusers;

}


Service:


public pageBean queryForPage(int pageSize,int currentPage);


ServiceImpl:


public pageBean queryForPage(int pageSize, int currentPage) {

// TODO Auto-generated method stub

pageBean pagebean=new pageBean();

System.out.println("cp1"+currentPage);

int currentpage=pageBean.countCurrentPage(currentPage);

System.out.println("cp2"+currentpage);


int totalRow=findAll().size();

int totalpage=pageBean.countTotalPage(totalRow, pageSize);

final int length=pageSize;

final int offset=pageBean.countOffset(currentpage, pageSize);

System.out.println("of"+offset);

final String hql="from TUsers where 1=1";

List<TUsers> tusers=userdao.queryForPage(hql, offset, length);

pagebean.setList(tusers);

pagebean.setPageSize(pageSize);

pagebean.setTotalPage(totalpage);

pagebean.setTotalRow(totalRow);

pagebean.setCurrentPage(currentpage);

pagebean.init();

return pagebean;

}

action:


附加属性及相应get,set方法:

private pageBean pagebean;

private int currentPage;

private int pageSize;

get,set方法在此略去

public String doshow(){

UserService userservice=servicemanager.getUserService();

setPageSize(3);

System.out.println("cp"+currentPage);

pagebean=userservice.queryForPage(pageSize, currentPage);

return "success";

}