闲来无事是,做个一个分页效果,给大家分享一下看看
pageBean中的 domain类
public class PageBean<T> { private int currentPageNo=1;//当前页面 private int pageSize;//每页有多少条数据 private int totalRecords;//总记录数 private int totalPageNo;//总页面数 private List<T> datas;//数据 //获取上一页的页码 public int getPreviewPageNo(){ if(currentPageNo<=1) { return 1; } return currentPageNo-1; } //返回下一页的页码 public int getNextPageNo(){ if(currentPageNo>=totalPageNo){ return totalPageNo; } return currentPageNo+1; } public int getCurrentPageNo() { return currentPageNo; } public void setCurrentPageNo(int currentPageNo) { this.currentPageNo = currentPageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; //获取总页面数 int totalPageNo=this.getTotalRecords()%pageSize==0? this.totalRecords/pageSize:this.totalRecords /pageSize+1; this.setTotalPageNo(totalPageNo); } public int getTotalPageNo() { return totalPageNo; } public void setTotalPageNo(int totalPageNo) { this.totalPageNo = totalPageNo; } public List<T> getDatas() { return datas; } public void setDatas(List<T> datas) { this.datas = datas; } }
dao层如下所示:
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> { private Class clazz; public BaseDaoImpl() { //从jvm虚拟中获取正在运行的当前类 DeptDaoImpl Class c=this.getClass(); //获取当前的运行类的父类 BaseDaoImpl Type genericSuperclass=c.getGenericSuperclass(); //判断是否是参数化类型 if(genericSuperclass instanceof ParameterizedType){ ParameterizedType parameterizedType= (ParameterizedType) genericSuperclass; //获取泛型中的类型列表,返回类型数组 Type[] types=parameterizedType.getActualTypeArguments(); clazz= (Class) types[0]; } } public void save(T t) { this.getHibernateTemplate().save(t); } public void delete(T t) { this.getHibernateTemplate().delete(t); } public void update(T t) { this.getHibernateTemplate().update(t); } public T loadById(Serializable id) { return (T)this.getHibernateTemplate().load(clazz,id); } public List<T> findAll() { //创建离线查询对象 DetachedCriteria dc=DetachedCriteria.forClass(clazz); return (List<T>) this.getHibernateTemplate().findByCriteria(dc); } public PageBean<T> findAllData(DetachedCriteria dc,int currentPageNo,int pageSize) { PageBean pageBean=new PageBean(); //设置当前页 pageBean.setCurrentPageNo(currentPageNo); //设置每页显示条数 pageBean.setPageSize(pageSize); //获取当前页下的所有数据 List<T> datas = (List<T>) this.getHibernateTemplate().findByCriteria(dc,(currentPageNo-1) * pageSize,pageSize); pageBean.setDatas(datas); //查询总记录数 Object o= this.getHibernateTemplate().find("SELECT COUNT(*) FROM Dept").get(0); long count=(Long)o; int a=(int)count; //将总记录数设置在pageBean对象中,以便计算总页数 pageBean.setTotalRecords(a); return pageBean; } }biz层如下:一定要记住事物提交最重要的事物提交:@Transactional
@Transactional public class PageBizImpl implements PageBiz { private PageDao pageDao; public PageBean<Dept> findAllData(DetachedCriteria dc, int currentPageNo, int pageSize) { return pageDao.findAllData(dc,currentPageNo,pageSize); } public void setPageDao(PageDao pageDao) { this.pageDao = pageDao; } }action层,一定要注意有setget方法的实现:public class PageAction extends ActionSupport { private PageBiz pageBiz; private int currentPageNo=1; private PageBean pageBean=new PageBean(); private int totalPageNo; public String getPage(){ DetachedCriteria dc=DetachedCriteria.forClass(Dept.class); pageBean=pageBiz.findAllData(dc,currentPageNo,3); return "success"; } public String getPageNo() throws IOException { DetachedCriteria dc=DetachedCriteria.forClass(Dept.class); pageBean=pageBiz.findAllData(dc,currentPageNo,3); sendJson(); return NONE; } public String before() throws IOException { System.out.println(currentPageNo); pageBean.setCurrentPageNo(currentPageNo); pageBean.setCurrentPageNo(pageBean.getPreviewPageNo()); System.out.println(pageBean.getCurrentPageNo()); DetachedCriteria dc=DetachedCriteria.forClass(Dept.class); pageBean=pageBiz.findAllData(dc,pageBean.getCurrentPageNo(),3); sendJson(); return NONE; } public String after() throws IOException { pageBean.setCurrentPageNo(currentPageNo); pageBean.setTotalPageNo(totalPageNo); System.out.println("No>>>>>>>>>>>>>"+currentPageNo); System.out.println("total>>>>>>>>>>"+totalPageNo); pageBean.setCurrentPageNo(pageBean.getNextPageNo()); DetachedCriteria dc=DetachedCriteria.forClass(Dept.class); pageBean=pageBiz.findAllData(dc,pageBean.getCurrentPageNo(),3); sendJson(); return NONE; } public void sendJson() throws IOException { List<Dept> depts=pageBean.getDatas(); HttpServletResponse response= ServletActionContext.getResponse(); String jsonStr= JSON.toJSONString(depts); //将文件的类型设置为json格式 response.setContentType("text/json"); PrintWriter pw=response.getWriter(); pw.write(jsonStr); } public void setPageBiz(PageBiz pageBiz) { this.pageBiz = pageBiz; } public int getCurrentPageNo() { return currentPageNo; } public void setCurrentPageNo(int currentPageNo) { this.currentPageNo = currentPageNo; } public PageBean<Dept> getPageBean() { return pageBean; } public void setPageBean(PageBean<Dept> pageBean) { this.pageBean = pageBean; } public int getTotalPageNo() { return totalPageNo; } public void setTotalPageNo(int totalPageNo) { this.totalPageNo = totalPageNo; } }最后web层,以及js jq动态获取:
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/8/4 Time: 16:11 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script> <script type="text/javascript"> $(function () { <c:forEach begin="1" end="${pageBean.totalPageNo}" var="pageNo"> $("#${pageNo}").click(function () { $("#pageNo").val(${pageNo}); $.get("${pageContext.request.contextPath}/page/pagegetPageNo.action",{currentPageNo:${pageNo}},function(data) { var tr="<tr>"; $(data).each(function(i,obj) { var td="<td>"+obj.deptNO+"</td>"+ "<td>"+obj.dname+"</td>"+"<td>"+obj.loc+"</td>"; tr=tr+td+"</tr>"; }) $("#deptTable tr:gt(0)").remove(); $("#deptTable").append(tr); }); status(); }) </c:forEach> $("#before").click(function () { var pageNo= parseInt($("#pageNo").val()); if(pageNo>=2){ $("#pageNo").val(pageNo-1); } $.get("${pageContext.request.contextPath}/page/pagebefore.action",{currentPageNo:pageNo},function(data) { var tr="<tr>"; $(data).each(function(i,obj) { var td="<td>"+obj.deptNO+"</td>"+ "<td>"+obj.dname+"</td>"+"<td>"+obj.loc+"</td>"; tr=tr+td+"</tr>"; }) $("#deptTable tr:gt(0)").remove(); $("#deptTable").append(tr); }); status(); }) $("#after").click(function () { var pageNo= parseInt($("#pageNo").val()); if(pageNo<=${pageBean.totalPageNo}-1){ $("#pageNo").val(pageNo+1); } $.get("${pageContext.request.contextPath}/page/pageafter.action",{currentPageNo:pageNo,totalPageNo:${pageBean.totalPageNo}},function(data) { var tr="<tr>"; $(data).each(function(i,obj) { var td="<td>"+obj.deptNO+"</td>"+ "<td>"+obj.dname+"</td>"+"<td>"+obj.loc+"</td>"; tr=tr+td+"</tr>"; }) $("#deptTable tr:gt(0)").remove(); $("#deptTable").append(tr); }); status(); }) }) function status() { var pageNo=parseInt(document.getElementById("pageNo").value); document.getElementById(pageNo).style.backgroundColor="red"; <c:forEach begin="1" end="${pageBean.totalPageNo}" var="pageNo"> if(pageNo!=${pageNo}){ document.getElementById("${pageNo}").style.backgroundColor="white"; } </c:forEach> } window.onload=function () { document.getElementById("1").style.backgroundColor="red"; } </script> </head> <body> ${pageBean.totalPageNo} <input type="hidden" value="1" id="pageNo"/> <a href="#" style="padding-left: 5px;border: 1px solid gray;text-decoration: none" id="before">上一页</a> <c:forEach begin="1" end="${pageBean.totalPageNo}" var="pageNo"> <a href="#" style="padding-left: 5px;border: 1px solid gray;text-decoration: none" id="${pageNo}">${pageNo}</a> </c:forEach> <a href="#" style="padding-left: 5px;border: 1px solid gray;text-decoration: none" id="after">下一页</a> <table> <table id="deptTable"> <tr> <td>部门编号</td> <td>部门名称</td> <td>部门位置</td> </tr> <c:forEach items="${pageBean.datas}" var="data"> <tr> <td>${data.deptNO}</td> <td>${data.dname}</td> <td>${data.loc}</td> </tr> </c:forEach>
</table> </table> </body> </html>
这样效果就出来了:希望这篇文档能对大家有所帮助!最后祝大家身体健康,万事如意!