实现全项目通用的分页,java分页方式参考。

    一直在寻找一个通用的分页代码,可以集成到DAO的sql语句,hql语句,hibernate分页等等页面只需要简单的加载通用分页jsp就可以,感谢夏哥给我的参考代码 ...附上心得。

1.先写通用的分页类,Pagenation.java
public class Pagenation {
	//总记录条数
	private int rowCount=0;
	//总页数
	private int pageCount=0;
	//当前页数
	private int currentpage=1;
	//页面显示数目
	private int pageSize=10;
	//action名称(实现)
	private String actionName;
	//查询语句
	private String sql;
	private String hql;
	
	//构造方法
	public Pagenation(){
		super();
	}
	//有参构造函数
	public Pagenation(int rowCount,int pageCount,int currentpage,int pageSize){
		super();
		this.currentpage=currentpage;
		this.rowCount=rowCount;
		this.pageCount=pageCount;
		this.pageSize=pageSize;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
		//很好的分页算法.也可以换成可以换成
		//this.pageCount=rowCount%this.pageSize==0?rowCount/this.pageSize:rowCount/this.pageSize+1;
		this.pageCount=(rowCount+this.pageSize-1)/this.pageSize;
	}
	//开始记录条数
	public Integer getStartRow(){
		return (getCurrentpage()-1)*getPageSize();
	}
	//结束记录条数
	public Integer getEndRow(){
		return getCurrentpage()*getPageSize();		
	}
	public int getPageCount() {
		return pageCount;
	} 
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getCurrentpage() {
		if(currentpage>pageCount)
			currentpage=pageCount;
		if(currentpage<1)
			currentpage=1;
		return currentpage;
	}
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getActionName() {
		return actionName;
	}
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getHql() {
		return hql;
	}
	public void setHql(String hql) {
		this.hql = hql;
	}
}


2.在DAO里面引入Pagenation的参数属性,我用的是hibernate自带的分页方法
public List getNewsByPage(Object object, Pagenation pagenation) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		StringBuffer stringBuffer = new StringBuffer(
				" from *** where 1=1");
		pagenation.setHql(stringBuffer.toString());
		Query query = session.createQuery(stringBuffer.toString());
		//初始值
                query.setFirstResult(pagenation.getStartRow());
                //每页显示的值
		query.setMaxResults(pagenation.getPageSize());
		List list=query.list();
		session.close();
		return list;
	}

3.Action层利用注入导入Pagenation类,实现action的分页访问
//分页显示出每个栏目的信息, 使用Hibernate分页方法
@SuppressWarnings({ "unchecked", "null" })
	public String getByModule() {
		module = searchService.getModule(id);
		counts=searchService.getCounts(module);
		//封装了总记录数,再封装了总条数
		pagenation.setRowCount(counts);
                //acion的name值,在struts.xml配置
		pagenation.setActionName("index2");
		list=searchService.getNewsByPage(object,pagenation);				
		return SUCCESS;
	}

4.通用的pagenation.jsp页面,关键的页面
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%>
<%@page import="com.t.global.common.Pagenation" %>
<script language="javascript">
function goPage(page){
        //给pagenation.currentpage赋值,改变后的值
	document.getElementById("currentpage").value=page;
	alert("/////"+page);
	document.pagenation.submit();
}
</script>
<form action="${pagenation.actionName}.action" name="pagenation" method="post" style="display:none">
	<input type="hidden" name="pagenation.rowCount" value="${pagenation.rowCount }"/>
	<input type="hidden" name="pagenation.currentpage" id="currentpage" value="${pagenation.currentpage }"/>
	<input type="hidden" name="pagenation.actionName" value="pagenation.actionName"/>
	<input type="hidden" name="pagenation.sql" value="${pagenation.sql }"/>
	<input type="hidden" name="pagenation.hql" value="${pagenation.hql }"/>
</form>
<p>
	<a href="#" onclick="goPage(1)" style="cursor: pointer;">首页</a>
	<a href="#" onclick="goPage(${pagenation.currentpage-1})" style="cursor: pointer;">上一页</a>
	当前第&nbsp;<font color="red">${pagenation.currentpage }</font>&nbsp;页
	<a href="#" onclick="goPage(${pagenation.currentpage+1})" style="cursor: pointer;">下一页</a>
	<a href="#" onclick="goPage(${pagenation.pageCount})" style="cursor: pointer;">末页</a>
	&nbsp;&nbsp;跳转到第<select onchange="goPage(this.value)">
	<option value="1"></option>
	<%
	Pagenation pagenation=(Pagenation)request.getAttribute("pagenation");
	Integer pageCount=pagenation.getPageCount();
	for(int i=1;i<=pageCount;i++){
	out.write("<option value=\""+i+"\">"+i+"</option>");
	}
	%>
	</select>页&nbsp;&nbsp;
</p>


5.往你需要的jsp页面嵌入上面的pagenation.jsp页面,一般放在页面的下面
    
<div style="font-size: 12px" align="center">
        <%@include file="pagenation.jsp" %>
      </div>

附件有分页的效果,其实,一直想改成1,2,3,4,5,...,10000的分页方式,希望有能改成的留个言,大家共同进步

你可能感兴趣的:(java,DAO,jsp,Hibernate)