实体bean---------> PageBean.java
package com.demo.page;import java.util.List;public class PageBean { /*==============================================================*/ private List resultList;//结果集 private int recordSUM;//总记录数 private int pageSUM;//总页数 private int currentPage;//当前页 private int pageSize;//页记录数 private boolean isFirstPage;//是否是第一页 private boolean isLastPage;//是否是最后一页 private boolean hasPreviousPage;//是否有上一页 private boolean hasNextPage;//是否有下一页 /*===========================初始化===================================*/ public void init(){ this.isFirstPage = isFirstPage(); this.isLastPage = isLastPage(); this.hasPreviousPage = isHasPreviousPage(); this.hasNextPage = isHasNextPage(); } /*=============================总页数=================================*/ public static int countTotalPage(int pageSize,int recordSUM){ int totalPage = recordSUM % pageSize == 0 ? recordSUM/pageSize : recordSUM/pageSize+1; return totalPage; } /*============================当前页开始记录号 ==================================*/ public static int countOffset(final int pageSize,final int currentPage){ final int offset = pageSize*(currentPage-1); return offset; } /*=============================当期页=================================*/ public static int countCurrentPage(int page){ final int curPage = (page==0?1:page); return curPage; } /*============================判断当前页的状态==================================*/ public boolean isFirstPage() { return currentPage == 1; } public boolean isLastPage() { return currentPage == pageSUM; } public boolean isHasPreviousPage() { return currentPage != 1; } public boolean isHasNextPage() { return currentPage != pageSUM; } /*===========================Get/Set方法===================================*/ public List getResultList() { return resultList; } public void setResultList(List resultList) { this.resultList = resultList; } public int getRecordSUM() { return recordSUM; } public void setRecordSUM(int recordSUM) { this.recordSUM = recordSUM; } public int getPageSUM() { return pageSUM; } public void setPageSUM(int pageSUM) { this.pageSUM = pageSUM; } 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; } /*==============================================================*/ }
JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'listUser.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <table border="" > <tr> <td><input type="checkbox"></td><td>用户名</td><td>密码</td><td>性别</td><td>年龄</td><td>邮箱</td><td>地址</td> </tr> <s:iterator value="resultList" id="user"> <tr> <td><input type="checkbox"></td> <td><s:property value="#user.username"/></td> <td><s:property value="#user.password"/></td> <td><s:property value="#user.sex"/></td> <td><s:property value="#user.age"/></td> <td><s:property value="#user.email"/></td> <td><s:property value="#user.address"/></td> </tr> </s:iterator> </table> 共<s:property value="pageBean.recordSUM"/> 条记录 共<s:property value="pageBean.pageSUM"/> 页 当前第<s:property value="pageBean.currentPage"/>页<br/> <s:if test="%{pageBean.currentPage == 1}"> 第一页 上一页 </s:if> <s:else> [url=showUser.action?page=1]第一页[/url] [url=showUser.action?page=<s:property value=]">上一页[/url] </s:else> <s:if test="%{pageBean.currentPage != pageBean.pageSUM}"> [url=showUser.action?page=<s:property value=]">下一页[/url] [url=showUser.action?page=<s:property value=]">最后一页[/url] </s:if> <s:else> 下一页 最后一页 </s:else> </body></html>
PageServiceImplForUser.java
package com.demo.page;import java.util.List;import com.demo.entity.dao.AdminiDao;import com.demo.entity.dao.UserDao;public class PageServiceImplForUser implements PageService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public PageBean queryForPage(int pageSize, int currentPage,String hql) { // TODO Auto-generated method stub int recordSUM = userDao.getAllRowCount(hql);//总记录数 int pageSUM = PageBean.countTotalPage(pageSize, recordSUM);//总页数 final int offset = PageBean.countOffset(pageSize, currentPage);//当前页开始记录 final int length = pageSize;//每页记录数 final int page = PageBean.countCurrentPage(currentPage);//当前页 List<AdminiDao> list = userDao.queryForPage(hql,offset, length);//"一页"的记录 //把分页信息保存到Bean中 PageBean pageBean = new PageBean(); pageBean.setPageSize(pageSize); pageBean.setCurrentPage(page); pageBean.setRecordSUM(recordSUM); pageBean.setPageSUM(pageSUM); pageBean.setResultList(list); pageBean.init(); return pageBean; }}
UserDaoImpl.java
package com.demo.entity.dao.Impl;import java.util.ArrayList;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.demo.entity.User;import com.demo.entity.dao.UserDao;import com.demo.hibernate.utils.HibernateSessionFactory;public class UserDaoImpl extends HibernateDaoSupport implements UserDao { public int getAllRowCount(String hql) { // TODO Auto-generated method stub Session session=HibernateSessionFactory.getSession(); int i=session.createQuery("from User").list().size(); return i; } public List queryForPage(String hql, int offset, int length) { // TODO Auto-generated method stub List list=new ArrayList(); Session session=HibernateSessionFactory.getSession(); Query query=session.createQuery(hql); query.setFirstResult(offset); query.setMaxResults(length); list=query.list(); return list; } public String addUser(User user) { // TODO Auto-generated method stub Session session=HibernateSessionFactory.getSession(); Transaction tx=session.beginTransaction(); session.save(user); tx.commit(); session.close(); return "success"; }}
ACTION---------> ShowUser.java
package com.demo.action;import java.util.ArrayList;import java.util.List;import com.demo.entity.dao.Impl.AdminiDaoImpl;import com.demo.entity.dao.Impl.UserDaoImpl;import com.demo.page.PageBean;import com.demo.page.PageService;import com.opensymphony.xwork2.ActionSupport;public class ShowUser extends ActionSupport { //通过applicationContext.xml配置文件注入UserService的值 private PageService pageService;//PAGE服务类 private int page; //当第几页 private PageBean pageBean;//包含分布信息的bean private UserDaoImpl userDaoImpl;//用户dao实现 private String hql;//查询SQL private int pageSize;//每页记录数 private List resultList=new ArrayList();//返回结果集 @Override public String execute() throws Exception { //分页的pageBean,参数pageSize表示每页显示记录数,page为当前页 pageBean = pageService.queryForPage(pageSize, page, hql); resultList=userDaoImpl.queryForPage(hql, getFirstNum()-1, pageSize); return "showUser"; } private int getFirstNum(){ if(page==1){ return 1; }else{ return (page-1)*20+1; } } /** * Get/Set方法 */ public PageService getPageService() { return pageService; } public void setPageService(PageService pageService) { this.pageService = pageService; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public PageBean getPageBean() { return pageBean; } public UserDaoImpl getUserDaoImpl() { return userDaoImpl; } public void setUserDaoImpl(UserDaoImpl userDaoImpl) { this.userDaoImpl = userDaoImpl; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } public String getHql() { return hql; } public void setHql(String hql) { this.hql = hql; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List getResultList() { return resultList; } public void setResultList(List resultList) { this.resultList = resultList; }}
spring中的配置
<!-- =========================================================================== --> <bean id="showUser" class="com.demo.action.ShowUser"> <property name="PageService" ref="pageServiceForUser"></property> <property name="pageBean" ref="pageBean"></property> <property name="UserDaoImpl" ref="userDao"></property> <property name="page" value="1"></property> <property name="pageSize" value="16"></property> <property name="hql" value="from User"></property> </bean> <!-- =========================================================================== -->