直接进入主题:
建一个工具类:
Page
public class Page {
//总页数
private int page_zong;
//有几条
private int tiao_size;
//当前页数
private int dangqian;
//总条数
private int tiao_zong;
public Page() {
// TODO Auto-generated constructor stub
}
public int getPage_zong() {
return page_zong;
}
public void setPage_zong(int page_zong) {
this.page_zong = page_zong;
}
public int getTiao_size() {
return tiao_size;
}
public void setTiao_size(int tiao_size) {
this.tiao_size = tiao_size;
}
public int getDangqian() {
return dangqian;
}
public void setDangqian(int dangqian) {
this.dangqian = dangqian;
}
public int getTiao_zong() {
return tiao_zong;
}
public void setTiao_zong(int tiao_zong) {
this.tiao_zong = tiao_zong;
}
public Page(int page_zong, int tiao_size, int dangqian, int tiao_zong) {
super();
this.page_zong = page_zong;
this.tiao_size = tiao_size;
this.dangqian = dangqian;
this.tiao_zong = tiao_zong;
}
}
创建一个IPageDao的接口:
public interface IPageDao {
//定义一个接口
public List<Page> findByPage(Page p);
}
创建出来实体类PageDaoImpl:
public class PageDaoImpl implements IPageDao {
public List<Page> findBypage(Page p) {
// TODO Auto-generated method stub
Session session=sessionFactory.getCurrentSession();
Criteria cri=session.createCriteria(Page.class);
cri.setFirstResult((p.getDangqian()-1)*p.getTiao_size()).setMaxResults(p.getTiao_size());
return cri.list();
}
}
创建出来业务逻辑层IPageService:
public interface IPageService {
//定义一个接口
public List<Page> findByPage(Page p);
}
创建出来业务逻辑层的实体类PageServiceImpl:
public class PageServiceImpl implements IPageService {
private IPageDao ipd;
public List<Page> findBypage(Page p) {
// TODO Auto-generated method stub
return ipd.findBypage(p);
}
}
创建一个Action层PageAction:
注意:这里需要封装!!所定义的类 但是我省略掉了
public class PersonnelAction extends ActionSupport {
private IPersonnelService ips;
//分页
private Page p=new Page();
private int ye;
private int ye_zong;
private int tiaoshu;
public String findbypage(){
//得到总条数
tiaoshu=2;
int tiao=ips.findAll().size();
//计算页数
if(tiao%tiaoshu!=0){ p.setPage_zong((tiao/tiaoshu)+1);}else{
p.setPage_zong(tiao/tiaoshu); }
p.setTiao_size(tiaoshu);
if(ye>p.getPage_zong()){
ye=p.getPage_zong();
}if(ye==0){
ye=1;
}
p.setDangqian(ye);
ye_zong=p.getPage_zong();
list_per=ips.findBypage(p);
return "show";
}
}
下面是JSP页面:
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body> //直接调用分页查询就可以,获取到他的第一页
<a href="findbypagep.action?ye=${1}">进入权限</a>
</body>
</html>
list_p 界面 :
注意:在table外边写上一页---下一页。。。。。这里我就省略掉了
<%@ 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>My JSP 'list_qx.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
//。。。。省略
<c:if test="${ye==1}"> <a href="findbypageqx.action?ye=${ye-1}">已最前</a></c:if>
<c:if test="${ye!=1}"> <a href="findbypageqx.action?ye=${ye-1}">上一页</a></c:if>
${ye } / ${ye_zong}
<c:if test="${ye==ye_zong}"><a href="findbypageqx.action?ye=${ye+1}">已最后</a></c:if>
<c:if test="${ye!=ye_zong}"><a href="findbypageqx.action?ye=${ye+1}">下一页</a></c:if>
</body>
</html>
对于本篇文章有不理解或者不怎么懂的地方可以在下方留言,我会给你解答,谢谢阅读!