分页查询是从数据中查询所有数据返回到页面中。许多地方地方都会运用到。今天要写代码是整合了S2SH框架进行开发的。需要有Struts2+Spring+Hibernate三个框架整合后进行的。我之前写过一篇S2SH整合开发基本配置教程,如果有不明白的地方可以点击这里进行跳转学习。效果图如下
首先,一个最基本分页查询需要有个pageBean,也就是分页导航。内容基本大同小异,只要掌握了分页导航也就明白了分页查询内容,我这里采用的是泛型一个pageBean适合本系统所有的分页查询,pageBean代码如下:
package com.netxintai.domain; import java.util.List; public class PageBean<T> { //已知数据 private int pageNum; //当前页,从请求那边传过来。 private int pageSize; //每页显示的数据条数。 private int totalRecord; //总的记录条数。查询数据库得到的数据 //需要计算得来 private int totalPage; // 总页数,通过totalRecord和pageSize计算可以得来 //开始索引,也就是我们在数据库中要从第几行数据开始拿,有了startIndex和pageSize, //就知道了limit语句的两个数据,就能获得每页需要显示的数据了 private int startIndex; //将每页要显示的数据放在list集合中 private List<T> list; //分页显示的页数,比如在页面上显示1,2,3,4,5页,start就为1,end就为5,这个也是算过来的 private int start; private int end; public PageBean(){ } //通过pageNum,pageSize,totalRecord计算得来tatalPage和startIndex //构造方法中将pageNum,pageSize,totalRecord获得 public PageBean(int pageNum,int pageSize,int totalRecord) { this.pageNum = (pageNum==0)?1:pageNum;; this.pageSize = pageSize; this.totalRecord = totalRecord; //totalPage 总页数 this.totalPage=(totalRecord%pageSize==0)?totalRecord / pageSize:totalRecord / pageSize +1; //开始索引 this.startIndex = (pageNum-1)*pageSize ; //显示每页数量,这里自己可以设置,想显示几页就自己通过下面算法修改 this.start = 1; this.end = 10; //显示页数的算法 if(totalPage <=10){ //总页数都小于10,那么end就为总页数的值了。 this.end = this.totalPage; }else{ //总页数大于10,那么就要根据当前是第几页,来判断start和end为多少了, this.start = pageNum - 5; this.end = pageNum + 4; if(start <=0){ //比如当前页是第1页,或者第2页,那么就不如和这个规则, this.start = 1; this.end = 10; } if(end > this.totalPage){ //比如当前页是倒数第2页或者最后一页,也同样不符合上面这个规则 this.end = totalPage; this.start = end - 5; } } } //get、set方法省略。 public int getPageNum(int page){ int pageNum=(page==0)?1:page; return pageNum; } }
Dao层实现接口对数据库进行分页查询操作
package com.netxintai.dao; import java.util.List; import javax.annotation.Resource; import org.hibernate.criterion.DetachedCriteria; import org.springframework.orm.hibernate5.HibernateTemplate; import com.netxintai.domain.User; public class UserDaoImpl implements UserDao<User> { @Resource(name="hibernateTemplate") private HibernateTemplate hibernateTemplate; @Override public void save(User user) { hibernateTemplate.save(user); } @Override public List<User> queryByPage(int offset, int pageSize) { // TODO Auto-generated method stub //对数据进行分页查询 DetachedCriteria criteria=DetachedCriteria.forClass(User.class); return (List<User>) hibernateTemplate .findByCriteria(criteria, offset, pageSize); } @Override public int getAllRowCount() { // TODO Auto-generated method stub //查询数据库User对象的总条目数 int rows = hibernateTemplate.find("from User").size(); return rows; } }
service层对数据进行逻辑计算
package com.netxintai.service; import java.util.List; import javax.annotation.Resource; import org.springframework.transaction.annotation.Transactional; import com.netxintai.dao.UserDao; import com.netxintai.domain.PageBean; import com.netxintai.domain.User; @Transactional public class UserServiceImpl implements UserService{ @Resource(name="userDao") private UserDao userDao; @Override public PageBean<User> findAllUserPage(int pageNum, int pageSize) { int totalRecord=userDao.getAllRowCount(); PageBean<User> pageBean = new PageBean<User>(pageNum, pageSize, totalRecord); int startIndex = pageBean.getStartIndex(); pageBean.setList(userDao.queryByPage(startIndex, pageSize)); return pageBean; } }
在Action中对数据进行封装转发到页面
package com.netxintai.action; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.netxintai.domain.PageBean; import com.netxintai.domain.User; import com.netxintai.service.UserService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); @Resource(name = "userService") private UserService userService; @Override public User getModel() { // TODO Auto-generated method stub return user; } public String findally() throws Exception { int pageNum; HttpServletRequest request = ServletActionContext.getRequest(); String str = request.getParameter("pageNum"); //判定pageNum是否从页面带入数据 if (str == null) { pageNum = 1; } else { pageNum = Integer.parseInt(str); } int pageSize = 5;//设置每页显示多少条数据 PageBean<User> pageBean = userService.findAllUserPage(pageNum, pageSize); request.setAttribute("pageBean", pageBean); return "findall"; } }
JSP页面显示数据 对数据进行显示
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function validate(form) { var page=form.pageNum.value; // var page = document.getElementsByName("pageNum")[0].value; var totalPage=${request.pageBean.totalPage }; var reg=/^\d+$/; if(reg.test(page)==true){ if(page >totalPage ){ alert("你输入的页数大于最大页数,系统将跳转至第一页"); window.document.location.href = "${pageContext.request.contextPath }/admin/userAction_findally?pageNum=1"; return false; } return true; }else { alert("不是纯数字!失败! 系统跳转至第一页"); window.document.location.href = "${pageContext.request.contextPath }/admin/userAction_findally?pageNum=1"; return false; } return true; } </script> <title>查询所有页面</title> </head> <body> <a href="${pageContext.request.contextPath }/UserAdd.jsp"><h1>继续添加</h1></a> <div hight="500"> <table width="900" border="1" cellpadding="0" cellspacing="0" align="center"> <tr align="center"> <th>ID</th> <th>登录名</th> <th>性别</th> <th>邮箱</th> <th>电话</th> <th>地址</th> <th>操作</th> </tr> <c:forEach items="${request.list }" var="user"> <tr align="center"> <td>${user.name }</td> <td>${user.shili }</td> <td>${user.tongshuai }</td> <td>${user.wuli }</td> <td>${user.zhili }</td> <td>${user.zhengzhi }</td> <td>${user.meili }</td> <td>${user.teji }</td> <td><a href="${pageContext.request.contextPath}/userAction_ById?sid=${user.s_id }">修改</a> <a href="${pageContext.request.contextPath}/userAction_delete?sid=${user.s_id }">删除</a> </td> </tr> </c:forEach> </table> </div> <br> <center> 共有${request.pageBean.totalRecord}个数据,共${request.pageBean.totalPage }页,当前为${request.pageBean.pageNum}页 <br /> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=1">首页</a> <%--如果当前页为第一页时,就没有上一页这个超链接显示 --%> <c:if test="${requestScope.pageBean.pageNum ==1}"> <c:forEach begin="${requestScope.pageBean.start}" end="${requestScope.pageBean.end}" step="1" var="i"> <c:if test="${requestScope.pageBean.pageNum == i}"> [${i}] </c:if> <c:if test="${requestScope.pageBean.pageNum != i}"> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${i}">[${i}] </a> </c:if> </c:forEach> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${requestScope.pageBean.pageNum+1}">下一页</a> </c:if> <%--如果当前页不是第一页也不是最后一页,则有上一页和下一页这个超链接显示 --%> <c:if test="${requestScope.pageBean.pageNum > 1 && requestScope.pageBean.pageNum < requestScope.pageBean.totalPage}"> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${requestScope.pageBean.pageNum-1}">上一页</a> <c:forEach begin="${requestScope.pageBean.start}" end="${requestScope.pageBean.end}" step="1" var="i"> <c:if test="${requestScope.pageBean.pageNum == i}"> [${i}] </c:if> <c:if test="${requestScope.pageBean.pageNum != i}"> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${i}">[${i}] </a> </c:if> </c:forEach> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${requestScope.pageBean.pageNum+1}">下一页</a> </c:if> <%-- 如果当前页是最后一页,则只有上一页这个超链接显示,下一页没有 --%> <c:if test="${requestScope.pageBean.pageNum == requestScope.pageBean.totalPage}"> <c:if test="${requestScope.pageBean.pageNum-1>0 }"> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${requestScope.pageBean.pageNum-1}">上一页</a> <c:forEach begin="${requestScope.pageBean.start}" end="${requestScope.pageBean.end}" step="1" var="i"> <c:if test="${requestScope.pageBean.pageNum == i}"> [${i}] </c:if> <c:if test="${requestScope.pageBean.pageNum != i}"> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${i}">[${i}]</a> </c:if> </c:forEach> </c:if> </c:if> <%--尾页 --%> <a href="${pageContext.request.contextPath}/admin/userAction_findally?pageNum=${requestScope.pageBean.totalPage}">尾页</a> </center> <br> <center> <form action="${pageContext.request.contextPath }/admin/userAction_findally" onsubmit="return validate(this);"> <font size="4">跳转至</font> <input type="text" size="2" name="pageNum" id="pages" value="">页 <input type="submit" value="跳转""> </form> </center> </body> </html>
Test e is twice a week bud that s also where u messed up so u did more than u thibk each week 60mg priligy