1、三大框架分页的实现:DAO 里写法:/查出页面要显示的字段 -分页代码 页面上的查询全部public List page(Integer pageno) / TODO Auto-generated method stublog.debug(“find TblNewsclass instance“);try String sql = “select new TblNews(id,title,pubdate,status) from TblNews“;Query query = getHibernateTemplate().getSessionFactory().getCurrentSession
2、().createQuery(sql);query.setFirstResult(pageno);query.setMaxResults(3); /设置每页显示的条数List result = query.list();log.debug(“delete successful“);return result; catch (RuntimeException re) log.error(“delete failed“, re);throw re;public Integer getRows() /查询出数据库里的条数/ TODO Auto-generated method stublog.deb
3、ug(“find TblNewsclass instance“);try String sql = “select count(*) from TblNews“;Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);List list = query.list();log.debug(“delete successful“);return (Long)list.get(0).intValue(); catch (RuntimeException re) log.
4、error(“delete failed“, re);throw re;Action 里的代码实现 :private Integer totalElements;/总记录数private Integer pageNow=1;/当前页private Integer pageSize=3;/当前页面的记录数private Integer pageCount;/总页数private Integer no;上面就个成员变量再给个 set()get ()方法;/查询全部public String findAll()throws ExceptionList list = newsService.page(
5、getRowsno(); /参数是算出的页面的总条数if(list.size()!=0)ActionContext.getContext().put(“news“,list);return “findAllSuccess“;elsereturn ERROR;/分页算法public Integer getRowsno()Integer totalElements = newsService.getRows();pageCount = (totalElements + pageSize - 1) / pageSize;/计算出总页数if(pageNow pageCount)pageNow = pageCount;no = (pageNow -1)*pageSize;/实际的记录开始数return no;Jsp 页面写法:/页 首页1“上一页上一页 下一页下一页尾页配置文件里写/tx标签代表时间传播/切入/这个名字要和action里定义的那个成员变量对应