分页貌似是Web开发中很老的一个问题了,但是我以前用的分页方法在解耦合上都不是很好,在最近的项目里对分页又改进了一下,几乎可以在任何需要分页的页面下做引用,并直接使用! 目前已成功用在列表的分页显示,及文章内容的分页显示上,这个page组件只需要一个pageBean和一个page.jsp。下面说一下实现:
我是在SSH环境下用的,就以这个环境为例说一下
pageBean类:
import java.util.List;
/**
* 分页组件 需先setResult
* @author Seven.Kyo
*
*/
public class PageBean {
private int pageNum =1; //当前页
private int maxResult; //总记录数
private int pageMax; //每页显示数
private int maxPage ; //总页数
public int getPageMax() {
return pageMax;
}
public void setPageMax(int pageMax) {
this.pageMax = pageMax;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getMaxPage() {
return maxPage;
}
public void setMaxPage() {
this.maxPage=(maxResult%pageMax==0 ? maxResult/pageMax : maxResult/pageMax + 1);
}
public int getMaxResult() {
return maxResult;
}
public void setMaxResult(int maxResult) {
this.maxResult = maxResult;
}
}
page.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
/**获得并判断当前窗口的地址,添加pageNum参数==解耦合的关键**/
function go(num)
{
if(num<=0)
{
alert('当前已是最前页');
return;
}
if(num>${page.maxPage})
{
alert('当前已是最后页');
return;
}
var url = window.location;
var pos = String(url).indexOf("pageNum"); //查看是否存在pageNum页数参数
if(pos=="-1")
{
window.location.replace(url+'&pageNum='+num) ; //不存在则添加,值为所点击的页数
}
else
{
var ui = String(url).substring(0,pos);
window.location.replace(ui+'pageNum='+num); //存在,则刷新pageNum参数值
}
}
</script>
</head>
<a
href="javascript:go('${page.pageNum -1}')">上一页</a>
<c:forEach begin="1" end="${page.maxPage}" var="tt">
<c:choose>
<c:when test="${tt==page.pageNum}">
<strong>${tt}</strong>
</c:when>
<c:otherwise>
<a
href="javascript:go('${tt }')">${tt}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<a
href="javascript:go('${page.pageNum +1 }')">下一页</a><a
href="javascript:go(${page.maxPage })">最后一页</a>共有${page.maxPage }页
</html>
在需要分页的页面中添加:<jsp:include page="page.jsp" flush="true"/>
Struts Action里面的数据处理:
PageBean page = new PageBean();
String pageNum = request.getParameter("pageNum");
if(pageNum==null)
{
pageNum = "1";
request.setAttribute("pageNum",pageNum);
}
else
{
request.setAttribute("pageNum", pageNum);
page.setPageNum(Integer.valueOf(pageNum));
}
page.setPageMax(5);//设置每页显示行数
List list = function(page); //调用查询数据的方法,带一个PageBean的参数page,page的Result属性在function(page)方法中被赋值,所以在查询数据时要多查一条数据总数的信息,赋给page的Result属性。
page.setMaxPage(); //最后设置page的总页数属性
request.setAttribute("page", page);//将page放在request范围内
底层查询数据,并为page的Result属性赋值的方法(本例使用Spring与Hibernate的整合):
public List getEntitys(final PageBean page)
{
// TODO Auto-generated method stub
return (List)this.getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)throws HibernateException, SQLException
{
// TODO Auto-generated method stub
Long max = (Long)session.createQuery("select count(id) from Entity").uniqueResult();//查询数据总数
page.setMaxResult(max.intValue());//为page的result属性赋值
return session.createQuery("HQL") .setFirstResult((page.getPageNum()-1)*page.getPageMax())
.setMaxResults(page.getPageMax()).list();
}
});
}
对于长篇文章的分页显示也同样适用,但要结合FCK的分页符功能,而且只要在Action中做一下文章的拆分处理就可以了,底层方法无需修改;
Action中的处理基本和上面的类似
PageBean page = new PageBean();
String pageNum = request.getParameter("pageNum");
if(pageNum==null)
{
page.setPageNum(1);
}
else
{
page.setPageNum(Integer.valueOf(pageNum));
}
String content = function();//获得查询到得文章内容
String[] strArray=content.split("<div> </div>");
//根据分页符,将文章分割为字符串数组
content = strArray[page.getPageNum()-1];
//取要显示的第一页文章内容赋给content
page.setMaxResult(strArray.length);
page.setPageMax(1);//每页显示条数固定设为1
page.setMaxPage();
//分别为pageBean的各个属性赋值
request.setAttribute("page", page);//将page放到request范围内
request.setAttribute("content",content);//要显示的文章内容
OK,大概就是这样了!!
已经过测试实现,有问题或任何建议,欢迎留言讨论!!! 转载注明出处。