{转}ssh超实用分页实现
程序员文章站
2022-07-02 17:53:31
...
http://shiyangxt.cnblogs.com
下面是分页思想,一个初始化方法和一个分页实现方法:
分页的util类:
package com.sy.util;
import java.util.List;
@SuppressWarnings("unchecked")
public class Page {
private int pageSize;
private int totalPage;
private int rowCount;
private int currentPage;
private int prePage;
private int nextPage;
private boolean hasNextPage;
private boolean hasPreviousPage;
private List list;
public Page() {
this.pageSize=10;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
import java.util.List;
@SuppressWarnings("unchecked")
public class Page {
private int pageSize;
private int totalPage;
private int rowCount;
private int currentPage;
private int prePage;
private int nextPage;
private boolean hasNextPage;
private boolean hasPreviousPage;
private List list;
public Page() {
this.pageSize=10;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
分页的数据库操作和逻辑判断我把他单独用一个PageDaoImpl来实现:
/*
* 施杨的分页daoimpl类
* */
package com.sy.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sy.dao.BaseDao;
import com.sy.util.Page;
@SuppressWarnings("unchecked")
public class PageDaoImpl extends HibernateDaoSupport {
private String hql;
public Page page;
public int start;
public BaseDao dao;
public void setDao(BaseDao dao) {
this.dao = dao;
}
public void init(int start,String tableName){ // ͨ��init����ʵ����ij�ʼ��
page = new Page();
this.hql = "from "+tableName;
this.start = start;
setRowCount();
setTotalPage();
setCurrentPage();
setPrePage();
setNextPage();
setPreOrNextBoolean();
}
public int getRowCount(){
List list = dao.find(hql);
if(list.isEmpty()){
return 0;
}
return list.size();
}
public Page getPage(){
List list = (List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());
return query.list();
}
});
page.setList(list);
return page;
}
public void setPreOrNextBoolean() {
if (page.getCurrentPage() <= 1) {
page.setHasPreviousPage(false);
} else {
page.setHasPreviousPage(true);
}
if (page.getCurrentPage() >= page.getTotalPage()) {
page.setHasNextPage(false);
} else {
page.setHasNextPage(true);
}
}
public void setCurrentPage() {
if (start < 1) {
page.setCurrentPage(1);
}
if (start > page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
}
page.setCurrentPage(start);
}
public void setPrePage() {
page.setPrePage(page.getCurrentPage() - 1);
}
public void setNextPage() {
page.setNextPage(page.getCurrentPage() + 1);
}
public void setTotalPage() {
int rowCount = getRowCount();
int pageSize = page.getPageSize();
if (rowCount > pageSize) {
if (rowCount % pageSize == 0) {
page.setTotalPage(rowCount / pageSize);
} else {
page.setTotalPage(1 + (rowCount / pageSize));
}
} else {
page.setTotalPage(1);
}
}
public void setRowCount() {
page.setRowCount(getRowCount());
}
public int getStartIndex() {
int startIndex = 0;
if (start < 0) {
startIndex = 0;
} else {
if (start > page.getTotalPage()) {
startIndex = page.getPageSize() * (page.getTotalPage() - 1);
} else {
startIndex = page.getPageSize() * (start - 1);
}
}
return startIndex;
}
}
* 施杨的分页daoimpl类
* */
package com.sy.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sy.dao.BaseDao;
import com.sy.util.Page;
@SuppressWarnings("unchecked")
public class PageDaoImpl extends HibernateDaoSupport {
private String hql;
public Page page;
public int start;
public BaseDao dao;
public void setDao(BaseDao dao) {
this.dao = dao;
}
public void init(int start,String tableName){ // ͨ��init����ʵ����ij�ʼ��
page = new Page();
this.hql = "from "+tableName;
this.start = start;
setRowCount();
setTotalPage();
setCurrentPage();
setPrePage();
setNextPage();
setPreOrNextBoolean();
}
public int getRowCount(){
List list = dao.find(hql);
if(list.isEmpty()){
return 0;
}
return list.size();
}
public Page getPage(){
List list = (List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());
return query.list();
}
});
page.setList(list);
return page;
}
public void setPreOrNextBoolean() {
if (page.getCurrentPage() <= 1) {
page.setHasPreviousPage(false);
} else {
page.setHasPreviousPage(true);
}
if (page.getCurrentPage() >= page.getTotalPage()) {
page.setHasNextPage(false);
} else {
page.setHasNextPage(true);
}
}
public void setCurrentPage() {
if (start < 1) {
page.setCurrentPage(1);
}
if (start > page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
}
page.setCurrentPage(start);
}
public void setPrePage() {
page.setPrePage(page.getCurrentPage() - 1);
}
public void setNextPage() {
page.setNextPage(page.getCurrentPage() + 1);
}
public void setTotalPage() {
int rowCount = getRowCount();
int pageSize = page.getPageSize();
if (rowCount > pageSize) {
if (rowCount % pageSize == 0) {
page.setTotalPage(rowCount / pageSize);
} else {
page.setTotalPage(1 + (rowCount / pageSize));
}
} else {
page.setTotalPage(1);
}
}
public void setRowCount() {
page.setRowCount(getRowCount());
}
public int getStartIndex() {
int startIndex = 0;
if (start < 0) {
startIndex = 0;
} else {
if (start > page.getTotalPage()) {
startIndex = page.getPageSize() * (page.getTotalPage() - 1);
} else {
startIndex = page.getPageSize() * (start - 1);
}
}
return startIndex;
}
}
然后是业务层接口,举例AdminService.java
//管理员service层接口
package com.sy.service;
import java.util.List;
import com.sy.util.Page;
import com.sy.vo.Admin;
public interface AdminService extends BaseService{
//分页初始化
public void init(int pno);
//分页实现
public Page getPage();
}
package com.sy.service;
import java.util.List;
import com.sy.util.Page;
import com.sy.vo.Admin;
public interface AdminService extends BaseService{
//分页初始化
public void init(int pno);
//分页实现
public Page getPage();
}
实现类AdminServiceImpl.java
//管理员service层实现类
package com.sy.service.impl;
import java.util.List;
import com.sy.dao.impl.AdminDaoImpl;
import com.sy.dao.impl.PageDaoImpl;
import com.sy.service.AdminService;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminServiceImpl extends BaseServiceImpl implements AdminService {
.
public Page getPage() {
return Pdao.getPage();
}
public void init(int pno) {
String tableName="Admin";
Pdao.init(pno,tableName);
}
}
package com.sy.service.impl;
import java.util.List;
import com.sy.dao.impl.AdminDaoImpl;
import com.sy.dao.impl.PageDaoImpl;
import com.sy.service.AdminService;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminServiceImpl extends BaseServiceImpl implements AdminService {
.
public Page getPage() {
return Pdao.getPage();
}
public void init(int pno) {
String tableName="Admin";
Pdao.init(pno,tableName);
}
}
struts的action层AdminAction.java
//管理员action实现类
package com.sy.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.sy.form.AdminForm;
import com.sy.form.LoginForm;
import com.sy.service.AdminService;
import com.sy.util.MD5;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminAction extends BaseAction {
.
//查看管理员
public ActionForward listAdmins(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminService as=(AdminService)super.getBean("AdminService");
int pno = (new Integer(request.getParameter("pno"))).intValue();
as.init(pno);
Page myPage = as.getPage();
List myList = as.getPage().getList();
request.setAttribute("myPage", myPage);
request.setAttribute("AdminList", myList);
return mapping.findForward("show");
}
}
package com.sy.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.sy.form.AdminForm;
import com.sy.form.LoginForm;
import com.sy.service.AdminService;
import com.sy.util.MD5;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminAction extends BaseAction {
.
//查看管理员
public ActionForward listAdmins(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminService as=(AdminService)super.getBean("AdminService");
int pno = (new Integer(request.getParameter("pno"))).intValue();
as.init(pno);
Page myPage = as.getPage();
List myList = as.getPage().getList();
request.setAttribute("myPage", myPage);
request.setAttribute("AdminList", myList);
return mapping.findForward("show");
}
}
结构清晰,也很实用。
页面lookAdmin.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>施杨SSH新闻发布</title>
</head>
<body>
<center>
<h3>
查看管理员
</h3>
<br>
<a href="./admin/addAdmin.jsp">添加管理员</a>
<br>
<a href="./NewsPage2.do?pno=1">查看新闻</a>
<br>
<table border="2" width="600">
<tr>
<td>
用户名
</td>
<td>
密码
</td>
<td>
操作
</td>
</tr>
<c:forEach items="${requestScope['AdminList']}" var="admin">
<tr align="center">
<td width="20%" height="10"><c:out value="${admin.aname}" /></td>
<td width="20%" height="10"><c:out value="${admin.apassword}" /></td>
<td>
<a href="./editAdmin.do?aid=<c:out value="${admin.aid}"/>">修改</a>
<a href="./deleteAdmin.do?aid=<c:out value="${admin.aid}"/>" onclick="javascript:return confirm('您确定删除吗?')">删除</a>
</td>
</tr>
</c:forEach>
</table>
<table align="center" width="500">
<tr>
<td align="center" colspan="10">
<logic:present name="myPage">
<html:link page="/AdminPage.do?pno=1">首页</html:link>
<logic:equal name="myPage" property="hasPreviousPage" value="false">上一页</logic:equal>
<logic:equal name="myPage" property="hasPreviousPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="prePage"/>">上一页</a>
</logic:equal>
每页<bean:write name="myPage" property="pageSize"/>条记录
共<bean:write name="myPage" property="rowCount"/>条记录
当前第(<bean:write name="myPage" property="currentPage"/>/<bean:write name="myPage" property="totalPage"/>)页
<logic:equal name="myPage" property="hasNextPage" value="false">下一页</logic:equal>
<logic:equal name="myPage" property="hasNextPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="nextPage"/>">下一页</a>
</logic:equal>
<a href="./AdminPage.do?pno=<bean:write name='myPage' property='totalPage'/>">末页</a>
</logic:present>
</td>
</tr>
</table>
</center>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>施杨SSH新闻发布</title>
</head>
<body>
<center>
<h3>
查看管理员
</h3>
<br>
<a href="./admin/addAdmin.jsp">添加管理员</a>
<br>
<a href="./NewsPage2.do?pno=1">查看新闻</a>
<br>
<table border="2" width="600">
<tr>
<td>
用户名
</td>
<td>
密码
</td>
<td>
操作
</td>
</tr>
<c:forEach items="${requestScope['AdminList']}" var="admin">
<tr align="center">
<td width="20%" height="10"><c:out value="${admin.aname}" /></td>
<td width="20%" height="10"><c:out value="${admin.apassword}" /></td>
<td>
<a href="./editAdmin.do?aid=<c:out value="${admin.aid}"/>">修改</a>
<a href="./deleteAdmin.do?aid=<c:out value="${admin.aid}"/>" onclick="javascript:return confirm('您确定删除吗?')">删除</a>
</td>
</tr>
</c:forEach>
</table>
<table align="center" width="500">
<tr>
<td align="center" colspan="10">
<logic:present name="myPage">
<html:link page="/AdminPage.do?pno=1">首页</html:link>
<logic:equal name="myPage" property="hasPreviousPage" value="false">上一页</logic:equal>
<logic:equal name="myPage" property="hasPreviousPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="prePage"/>">上一页</a>
</logic:equal>
每页<bean:write name="myPage" property="pageSize"/>条记录
共<bean:write name="myPage" property="rowCount"/>条记录
当前第(<bean:write name="myPage" property="currentPage"/>/<bean:write name="myPage" property="totalPage"/>)页
<logic:equal name="myPage" property="hasNextPage" value="false">下一页</logic:equal>
<logic:equal name="myPage" property="hasNextPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="nextPage"/>">下一页</a>
</logic:equal>
<a href="./AdminPage.do?pno=<bean:write name='myPage' property='totalPage'/>">末页</a>
</logic:present>
</td>
</tr>
</table>
</center>
</body>
</html>
上一篇: {转}HibernateCallback
下一篇: axios请求设置header信息方法