直接進入主題:
建一個工具類:
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%>" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<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" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
-->
</head>
<body> //直接調用分頁查詢就可以,擷取到他的第一頁
<a href="findbypagep.action?ye=${1}" target="_blank" rel="external nofollow" >進入權限</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%>" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<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" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
-->
</head>
<body>
//。。。。省略
<c:if test="${ye==1}"> <a href="findbypageqx.action?ye=${ye-1}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >已最前</a></c:if>
<c:if test="${ye!=1}"> <a href="findbypageqx.action?ye=${ye-1}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >上一頁</a></c:if>
${ye } / ${ye_zong}
<c:if test="${ye==ye_zong}"><a href="findbypageqx.action?ye=${ye+1}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >已最後</a></c:if>
<c:if test="${ye!=ye_zong}"><a href="findbypageqx.action?ye=${ye+1}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >下一頁</a></c:if>
</body>
</html>
對于本篇文章有不了解或者不怎麼懂的地方可以在下方留言,我會給你解答,謝謝閱讀!