1、定义通用分页modelpackage cn.com.bank.mmpf.model;
import cn.com.bank.mmpf.util.Constants;
/** * Created by IntelliJ IDEA. * User: willsu * Date: 2005-6-10 * Time: 16:51:22 * To change this template use File | Settings | File Templates. */public class PageControl implements java.io.Serializable{ private int pageno=1; //当前页码 private int pagesize=Constants.PAGE_SIZE_DEFALUT; //每页行数 private int rowcount; //总行数 private int pagecount; //总页数 private boolean useprevious;//前一页是否能用 private boolean usebehind;//后一页是否能用 private boolean usepage=true;//是否分页
public int getPageno() { return pageno; }
public void setPageno(int pageno) { this.pageno = pageno; }
public int getPagesize() { return pagesize; }
public void setPagesize(int pagesize) { if (pagesize==0){//0-->不分页 usepage=false; pagecount=1; pageno=1; useprevious=false; usebehind=false; } this.pagesize = pagesize; }
public int getRowcount() { return rowcount; }
public void setRowcount(int rowcount) { this.rowcount = rowcount; if(usepage){ pagecount=(rowcount % pagesize ==0 )?(rowcount /pagesize ) : ( rowcount / pagesize +1); useprevious=pageno==1?false:true; usebehind=pageno==pagecount?false:true; } }
public int getPagecount() { return pagecount; }
public void setPagecount(int pagecount) { this.pagecount = pagecount; }
public boolean isUseprevious() { return useprevious; }
public void setUseprevious(boolean useprevious) { this.useprevious = useprevious; }
public boolean isUsebehind() { return usebehind; }
public void setUsebehind(boolean usebehind) { this.usebehind = usebehind; }
public boolean isUsepage() { return usepage; }
public void setUsepage(boolean usepage) { this.usepage = usepage; }
}2、hibernate分页实现函数--〉放在底层类中 public class CommonServiceImpl extends HibernateDaoSupport implements CommonService{ 。。。 //通用分页处理 public List getListForPage(final String hsql,PageControl pageControl) throws Exception { List pagelist=null; Session session=getSession(); try { pageControl.setRowcount(getTotalCount(session,hsql)); //更新pagecontrol的各种属性 important if(pageControl.isUsepage()){ //是否分页 // sql=hsql+ " limit " + (pageNog-1)*page_size + "," +page_size; // offices= getHibernateTemplate().find(sql); //被屏蔽掉的是原来的处理 //以下是改过的处理方法 Query query=null; query=session.createQuery(hsql); int start=(pageControl.getPageno()-1)*pageControl.getPagesize(); int rowNum=pageControl.getPagesize(); query.setFirstResult(start); query.setMaxResults(rowNum); pagelist = query.list(); }else{ pagelist=session.find(hsql); } } finally { session.close(); } return pagelist; }
//计算表的总行数 private int getTotalCount(Session session, String hql) throws Exception { Integer amount = new Integer(0); int sql_from = hql.indexOf("from"); int sql_orderby=hql.indexOf("order by");//为了改进 String countStr=""; if(sql_orderby>0) { countStr="select count(*) "+hql.substring(sql_from,sql_orderby); } else countStr = "select count(*) "+hql.substring(sql_from);
Query query = session.createQuery(countStr); if(!query.list().isEmpty()){ amount = (Integer) query.list().get(0); } else return 0; return amount.intValue(); }}3、实际service对象继承commonserviceimplpublic class LogServiceImpl extends CommonServiceImpl implements LogService{ public List getLogByType(Integer logtype, PageControl pageControl) throws Exception{ String hql="from "+Systemlog.class.getName()+" as a where a.baselogtype.id="+logtype+" order by a.opertime desc"; return getListForPage(hql,pageControl); }}4、action实现public class LogListAction extends BaseAction{ public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { String forward =Constants.FORWORD_FIAL; LogService service = (LogService) getWebApplicationContext().getBean(Constants.LOG_SERVICE);//获得service对象 Object obj=((DynaActionForm) actionForm).get(Constants.PAGE_CONTROL_MODEL); Object obj1=((DynaActionForm) actionForm).get(Constants.PAGE_FILTER_ID); Integer filterid=obj1==null?new Integer(1):(Integer)obj1; PageControl pageControl=obj==null?new PageControl():(PageControl)obj; try { List mainlist=service.getLogByType(filterid,pageControl); //调用分页方法得到list List filterlist=service.loadAll(Baselogtype.class); ((DynaActionForm) actionForm).set(Constants.PAGE_ENTITY_LIST, mainlist); ((DynaActionForm) actionForm).set(Constants.PAGE_FILTER_LIST, filterlist); ((DynaActionForm) actionForm).set(Constants.PAGE_FILTER_ID,filterid); ((DynaActionForm) actionForm).set(Constants.PAGE_CONTROL_MODEL,pageControl); forward = Constants.FORWORD_SUCCESS; } catch (Exception e) { ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(Constants.ERROR_LIST,e.getMessage())); saveErrors(request, errors); } return actionMapping.findForward(forward); }}5、jsp页面5.1、分页部分代码: <logic:greaterThan value="0" name="LogListForm" property="pagecontrolmodel.rowcount"> <p align="right" style="font-size:12px"> 共<bean:write name="LogListForm" property="pagecontrolmodel.rowcount"/>行 共<bean:write name="LogListForm" property="pagecontrolmodel.pagecount"/>页 每页行数 <html:select name="LogListForm" property="pagecontrolmodel.pagesize" styleClass="DropDownList" style="width:80px" onchange="refresh()"> <html:option value="0">不分页</html:option> <html:option value="10">10行</html:option> <html:option value="20">20行</html:option> <html:option value="30">30行</html:option> <html:option value="50">50行</html:option> <html:option value="100">100行</html:option> </html:select> <logic:equal value="true" name="LogListForm" property="pagecontrolmodel.usepage"> <logic:equal value="true" name="LogListForm" property="pagecontrolmodel.useprevious"> <a href="javascript:goto(1)">首页</a> <a href="javascript:goto(<bean:write property="pagecontrolmodel.pageno" name="LogListForm"/>-1)">前一页</a> </logic:equal> <logic:equal value="false" name="LogListForm" property="pagecontrolmodel.useprevious"> 首页 前一页 </logic:equal>
<logic:equal value="true" name="LogListForm" property="pagecontrolmodel.usebehind"> <a href="javascript:goto(<bean:write property="pagecontrolmodel.pageno" name="LogListForm"/>+1)">后一页</a> <a href="javascript:goto(<bean:write property="pagecontrolmodel.pagecount" name="LogListForm"/>)">末页</a> </logic:equal>
<logic:equal value="false" name="LogListForm" property="pagecontrolmodel.usebehind"> 后一页 末页 </logic:equal> 跳至<html:text property="pagecontrolmodel.pageno" name="LogListForm" styleClass="TextBox" style="width:20px"/><input type="button" value="go" onclick="check();"> </logic:equal> </p> </logic:greaterThan> 5.2、分页实现配套javascript <script> function refresh() { document.all("LogListForm").submit(); }
function check() { var pageno=document.all("pagecontrolmodel.pageno"); if(!isInt(pageno.value)){alert("非法页数");return;} var ipageno=parseInt(pageno.value); var imaxno=parseInt(document.all("pagecontrolmodel.pagecount").value); if(ipageno<1 || ipageno>imaxno){ alert("页数不在允许的范围内!");return; } refresh(); }
function goto(pageno){ document.all("pagecontrolmodel.pageno").value=pageno; check(); } </script>
昵称: [登录] [注册]
主页:
邮箱:(仅博主可见)
验证码: 看不清,换一个
评论内容:
登录 注册
[使用Ctrl+Enter键快速提交评论]
Powered by: 博客园 Copyright © willsu