之前使用许多技术做过数据的分页显示功能,像pager-taglib分页框架,还有许多前端技术自带的分页.但很少使用框架自己尝试写分页,剖析分页的原理.现在自己实现一个数据分页的功能.
工程结构:
代码:
#数据模型层
User.java
package com.sunline.entity;
/**
* User entity. @author MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer userId;
private String userName;
private String userPassword;
private String userKind;
private Double userBalance;
private String userStatus;
// Constructors
/** default constructor */
public User() {
}
/** minimal constructor */
public User(String userName, String userPassword, String userKind, Double userBalance) {
this.userName = userName;
this.userPassword = userPassword;
this.userKind = userKind;
this.userBalance = userBalance;
}
/** full constructor */
public User(String userName, String userPassword, String userKind, Double userBalance, String userStatus) {
this.userName = userName;
this.userPassword = userPassword;
this.userKind = userKind;
this.userBalance = userBalance;
this.userStatus = userStatus;
}
// Property accessors
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return this.userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserKind() {
return this.userKind;
}
public void setUserKind(String userKind) {
this.userKind = userKind;
}
public Double getUserBalance() {
return this.userBalance;
}
public void setUserBalance(Double userBalance) {
this.userBalance = userBalance;
}
public String getUserStatus() {
return this.userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
}
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.sunline.entity.User" table="user" catalog="bank">
<id name="userId" type="java.lang.Integer">
<column name="user_id" />
<generator class="native"></generator>
</id>
<property name="userName" type="java.lang.String">
<column name="user_name" length="50" not-null="true" />
</property>
<property name="userPassword" type="java.lang.String">
<column name="user_password" length="50" not-null="true" />
</property>
<property name="userKind" type="java.lang.String">
<column name="user_kind" length="50" not-null="true" />
</property>
<property name="userBalance" type="java.lang.Double">
<column name="user_balance" precision="15" not-null="true" />
</property>
<property name="userStatus" type="java.lang.String">
<column name="user_status" length="4" />
</property>
</class>
</hibernate-mapping>
#数据访问层
package com.sunline.dao;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sunline.entity.User;
@Resource(name="UserDao")
public class UserDao extends HibernateDaoSupport{
/*
* 获取所有用户信息
*/
@SuppressWarnings("unchecked")
public List<User> findAll(){
String sql="from User";
List<User> list=this.getHibernateTemplate().find(sql);
return list;
}
}
#业务逻辑层
package com.sunline.biz;
import java.util.List;
import org.springframework.stereotype.Service;
import com.sunline.dao.UserDao;
import com.sunline.entity.User;
@Service(value="UserBiz")
public class UserBiz {
UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/*
* 获取所有用户信息
*/
@SuppressWarnings("unchecked")
public List<User> findAll(){
return userDao.findAll();
}
}
#数据分页的处理
Page.java
package com.sunline.entity;
import java.util.ArrayList;
import java.util.List;
public class Page {
private List<User> bigList;//分页数据集
private List<User> smallList;//返回实际的数据集
private int currentPage;//当前页
private int pageRow;//每页行数
private int pageTotal;//总页数
private int nextPage;//下一页
private int upPage;//上一页
private boolean isFirstPage;//是否为首页
private boolean isLastPage;//是否尾页
private int totalRow;//查询到的总记录数
public boolean getIsFirstPage() {
if(currentPage==1){
isFirstPage=true;
}
return isFirstPage;
}
public boolean getIsLastPage() {
this.getPageTotal();
if(currentPage==pageTotal){
isLastPage=true;
}
return isLastPage;
}
public int getNextPage() {
this.nextPage=currentPage+1;
return nextPage;
}
public int getUpPage() {
this.upPage=currentPage-1;
return upPage;
}
public int getPageTotal() {
this.pageTotal=bigList.size()/pageRow;
if(bigList.size()%pageRow!=0){
this.pageTotal++;
}
return pageTotal;
}
public List<User> getSmallList() {
smallList=new ArrayList();
for (int i=(currentPage-1)*pageRow;i<=currentPage*pageRow-1&&i<bigList.size();i++) {
smallList.add(bigList.get(i));
}
return smallList;
}
public void setBigList(List<User> bigList) {
this.bigList = bigList;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageRow(int pageRow) {
this.pageRow = pageRow;
}
public int getTotalRow() {
return this.bigList.size();
}
public int getPageRow() {
return pageRow;
}
public int getCurrentPage() {
return currentPage;
}
}
#控制层
UserAction.java
package com.sunline.action;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sunline.biz.UserBiz;
import com.sunline.entity.Page;
import com.sunline.entity.User;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = -3661658944790352760L;
//获取配置文件
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取配置中的实例
UserBiz userBiz = (UserBiz) context.getBean("UserBiz");
ActionContext contextq = ActionContext.getContext();
Map<String, Object> session = contextq.getSession();
private String currentPage; //前端传来的当前页码
public String getCurrentPage() {
return currentPage;
}
public void setCurrentPage(String currentPage) {
this.currentPage = currentPage;
}
public String execute() throws Exception {
// TODO Auto-generated method stub
List<User> list = (List<User>) userBiz.findAll();
//得到当前页
String currentPage = this.currentPage;
if(currentPage==null){
currentPage="1";
}
int pageNum=Integer.parseInt(currentPage);
Page page = new Page();
page.setBigList(list);//设置要分页的集合
page.setCurrentPage(pageNum);//设置当前页
page.setPageRow(5);//设置每页数量
session.put("page", page);
return SUCCESS;
}
}
#前端视图层
page.jsp
<%@page import="com.sunline.entity.User"%>
<%@page import="com.sunline.entity.Page"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>分页显示数据</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" href="bootstrap-3.3.7/css/bootstrap.min.css">
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function check(){
var currentPage = document.getElementById("currentPages").value;
var PageTotal = document.getElementById("PageTotal").value;
if(currentPage==""){
alert("请输入要跳转的页数!");
return false;
}
if(currentPage>PageTotal){
alert("对不起,没有你要查找的数据!");
return false;
}
window.location.href="${pageContext.request.contextPath}/Page?currentPage="+currentPage;
}
</script>
</head>
<body>
<div class="container-fluid row form-group">
<div class="panel panel-info">
<div class="panel-heading">
<strong class="glyphicon glyphicon-home" style="text-align: left;">数据分页</strong>
</div>
<div class="panel-body">
<table class="table table-hover" border="1">
<tr style="height: 40px;" class="success">
<td>用户ID</td>
<td>用户姓名</td>
<td>用户密码</td>
<td>账户类型</td>
<td>账户余额</td>
<td>账户状态</td>
</tr>
<%
Page pages = (Page)request.getSession().getAttribute("page");
System.out.println("你的页面数据为 : " + pages.getCurrentPage());
List<User> list = (List<User>) pages.getSmallList();
for(User user : list){
%>
<tr>
<td><%=user.getUserId() %></td>
<td><%=user.getUserName() %></td>
<td><%=user.getUserPassword() %></td>
<td><%=user.getUserKind() %></td>
<td><%=user.getUserBalance() %></td>
<td><%=user.getUserStatus() %></td>
</tr>
<%
}
%>
</table>
<div style="float: right;">
共 <font color="red"><%=pages.getTotalRow() %></font> 条数据
当前第 <font color="red"><%=pages.getCurrentPage() %></font> 页
共 <font color="red"><%=pages.getPageTotal() %></font> 页
<%
int yeshu = pages.getPageTotal();
int currentPage = pages.getCurrentPage();
%>
<a href="${pageContext.request.contextPath }/Page?currentPage=1">首页</a>
<%
if(currentPage!=1){
%>
<a href="${pageContext.request.contextPath }/Page?currentPage=<%=currentPage-1%>">上一页</a>
<%
}
%>
<%
if(currentPage<pages.getPageTotal()){
%>
<a href="${pageContext.request.contextPath }/Page?currentPage=<%=currentPage+1%>">下一页</a>
<%
}
%>
<label>跳转到</label>
<input type="text" name="currentPages" id="currentPages" style="width: 40px;"/>
<input type="hidden" name="PageTotal" id="PageTotal" value="<%=pages.getPageTotal()%>">
<label>页</label>
<input type="button" value="跳转" class="btn btn-success" οnclick="check()"/>
</div>
</div>
</div>
</div>
</body>
</html>
#截图
工程源码,需要参考可以下载使用,有使用教程,点击----->下载源码