财哥的地盘

家穷人丑,一米四九

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  21 Posts :: 19 Stories :: 3 Comments :: 0 Trackbacks

自己写个分页,用起来挺爽
PageBean类:   封装一切页面元素及结果集,保存在Session里,省略了一切能省略的setter getter

package com.yls.car.bean.car;

//通用Hibernate分页类
 
import java.util.*;

import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.HibernateException;
import org.apache.struts.util.LabelValueBean;

public class PageBean {
     
    
private int currentPage = 0//当前页
    private int totalPages = 0//总页数
    private int pageRecords = 0//每页显示记录数
    private int totalRecords = 0//总记录数
    private int startRecord = 0//当前页的第1条记录在数据库中的起始行
    private boolean hasNextPage = false//是否有下一页
    private boolean hasPreviousPage = false//是否有上一页    
    private List resultList = null;  //返回的分页结果集
    private Query query = null;
    
    
private ArrayList pages = new ArrayList(); //对应页面的html:select框
    private int selectPage = 0//对应页面所选的页面
    
    
public PageBean() {
    }

    
    
public PageBean(String hql,Session session,int _pageRecords) throws HibernateException {
        
//第一次分页,给类属性赋值
        query = session.createQuery(hql);
        currentPage 
= 1;
        pageRecords 
= _pageRecords;
        totalRecords 
= query.list().size(); 
        
if (totalRecords % pageRecords == 0{
            totalPages 
= totalRecords/pageRecords;
        }
 else {
            totalPages 
= totalRecords/pageRecords + 1;
        }

        startRecord 
= 0;
        setHasPreviousPage();
        setHasNextPage();        
        setPages();
        dividePage();
    }

    
    
//分页并将结果集做为PageBean一个属性
    public void dividePage() throws HibernateException {
        query.setFirstResult(startRecord);
        query.setMaxResults(pageRecords);
        resultList 
= query.list();
    }

    
    
//判断动作
    public void judgeAction(String action) {
        
if (action.equals("firstPage")) {
            currentPage 
= 1;
            startRecord 
= 0;
            setHasPreviousPage();
            setHasNextPage();
            dividePage();
        }
 else if (action.equals("previousPage"&& currentPage-1>0{
            currentPage 
= currentPage-1;
            setStartRecord();
            setHasPreviousPage();
            setHasNextPage();
            dividePage();
        }
 else if (action.equals("nextPage"&& currentPage<totalPages) {
            currentPage 
= currentPage+1;
            setStartRecord();
            setHasPreviousPage();
            setHasNextPage();
            dividePage();
        }
 else if (action.equals("lastPage")) {
            currentPage 
= totalPages;
            setStartRecord();
            setHasPreviousPage();
            setHasNextPage();
            dividePage();
        }
 else if (isNumber(action)) {
            currentPage 
= Integer.parseInt(action);
            setStartRecord();
            setHasPreviousPage();
            setHasNextPage();
            dividePage();
        }

    }

    
    
//判断是否是数字
    public boolean isNumber(String number) {
        
try {
            Integer.parseInt(number);
            
return true;
        }
 catch (Exception e) {
            e.printStackTrace();
            
return false;
        }

    }

    
    
//返回当前页
    public int getCurrentPage() {
        
return currentPage;
    }

    
    
//获得是否有下页,用于页面是否显示"下一页"
    public boolean isHasNextPage() {
        
return hasNextPage;
    }

    
    
//设置是否有下页
    private void setHasNextPage() {
        
if (currentPage < totalPages) {
            hasNextPage 
= true;
        }
 else {
            hasNextPage 
= false;
        }

    }

    
    
//获得是否有上页,用于页面是否显示"上一页"
    public boolean isHasPreviousPage() {
        
return hasPreviousPage;
    }

    
    
//设置是否有上页
    private void setHasPreviousPage() {
        
if (currentPage-1 > 0{
            hasPreviousPage 
= true;
        }
 else {
            hasPreviousPage 
= false;
        }

    }

    
    
//设置从哪行取记录
    public void setStartRecord() {
        startRecord 
= (currentPage-1)*pageRecords;
    }

    
    
//获得总页数
    public int getTotalPages() {
        
return totalPages;
    }

    
    
//返回结果集给页面的迭代器
    public List getResultList() {
        
return resultList;
    }

    
    
//返回集合给html:optionsCollection
    public ArrayList getPages() {
        
return pages;
    }

    
    
//设置页面下拉框的集合
    public void setPages() {
        
for(int i=0;i<totalPages;i++{
            pages.add(
new LabelValueBean(String.valueOf(i+1),String.valueOf(i+1)));
        }

    }

    
    
//获取所选的页面
    public int getSelectPage() {
        
return selectPage;
    }

}


Action中调用(只需传入3个参数hibernate中的session,HQL,还有每页分多少行数据,一切搞定)

public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        Session ss 
= null;
        HttpSession session 
= null;
        PageBean page 
= null;
        
try {
            ss 
= HibernateSessionFactory.getSession();
            session 
= request.getSession();
            String action 
= request.getParameter("action");
            String selectPage 
= request.getParameter("selectPage");
            
if (action == null && selectPage == null{
                page 
= new PageBean("FROM YcAccident", ss, 10);
                page.dividePage();
                session.setAttribute(
"page", page);
            }
 else {
                
if (session.getAttribute("page"!= null && action != null{
                    page 
= (PageBean) session.getAttribute("page");
                    page.judgeAction(action);
                }
 else if (session.getAttribute("page"!= null
                        
&& selectPage != null{
                    page 
= (PageBean) session.getAttribute("page");
                    page.judgeAction(selectPage);
                }

            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }

        
return mapping.findForward("querySuccess");
    }


页面显示

<table border="1">
                
<tr>
                    
<td>流水号</td>
                    
<td>类型</td>
                    
<td>我方车牌号</td>
                    
<td>我方驾驶者</td>
                    
<td>对方车牌号</td>
                    
<td>对方驾驶者</td>
                    
<td>发生时间</td>
                    
<td>发生地点</td>
                    
<td>处理交警</td>
                    
<td align="center">操作</td>
                
</tr>
            
<logic:iterate name="page" property="list" id="YcAccident"
                type
="com.yls.car.bean.hibernate.YcAccident">
                
                
<tr>
                    
<td><bean:write name="YcAccident" property="aid" /></td>
                    
<td><bean:write name="YcAccident" property="type" /></td>
                    
<td><bean:write name="YcAccident" property="myCarSign" /></td>
                    
<td><bean:write name="YcAccident" property="myDriver" /></td>
                    
<td><bean:write name="YcAccident" property="oppCarSign" /></td>
                    
<td><bean:write name="YcAccident" property="oppDriver" /></td>
                    
<td><bean:write name="YcAccident" property="occurTime" /></td>
                    
<td><bean:write name="YcAccident" property="occurAddress" /></td>
                    
<td><bean:write name="YcAccident" property="disposer" /></td>
                    
                    
<td>
                    
<input type="submit" value="删除" onClick="del(<bean:write name="YcAccident" property="aid" />)">
                    
<input type="button" value="详细" onClick="modify('<bean:write name="YcAccident" property="aid" />',
                                                                     '<bean:write name="YcAccident" property="type" />',
                                                                     
'<bean:write name="YcAccident" property="myCarSign" />',
                                                                     
'<bean:write name="YcAccident" property="myDriver" />',
                                                                     
'<bean:write name="YcAccident" property="oppCarSign" />',
                                                                     
'<bean:write name="YcAccident" property="oppDriver" />',
                                                                     
'<bean:write name="YcAccident" property="oppCarModel" />',
                                                                     
'<bean:write name="YcAccident" property="oppOrg" />',
                                                                     
'<bean:write name="YcAccident" property="occurTime" />',
                                                                     
'<bean:write name="YcAccident" property="occurAddress" />',
                                                                     
'<bean:write name="YcAccident" property="disposeDepart" />',
                                                                     
'<bean:write name="YcAccident" property="disposer" />',
                                                                     
'<bean:write name="YcAccident" property="scene" />',
                                                                     
'<bean:write name="YcAccident" property="damnify" />',
                                                                     
'<bean:write name="YcAccident" property="disposeResult" />',
                                                                     
'<bean:write name="YcAccident" property="myResponsibility" />',
                                                                     
'<bean:write name="YcAccident" property="punishAmount" />',
                                                                     
'<bean:write name="YcAccident" property="myAmount" />',
                                                                     
'<bean:write name="YcAccident" property="oppAmount" />',
                                                                     
'<bean:write name="YcAccident" property="insuranceAmount" />')">
                    </td>                                                            
                
</tr>
            
</logic:iterate>
            
<tr>
                
<td align="right" colspan="10">
                    
<logic:equal name="page" property="hasPreviousPage" value="true">
                        
<html:link page="/showAccident.do?action=previousPage">上一页</html:link>
                    
</logic:equal> 
                    
<logic:equal name="page" property="hasNextPage" value="true">
                        
<html:link page="/showAccident.do?action=nextPage">下一页</html:link>
                    
</logic:equal>
                    总页数:
<bean:write name="page" property="totalPages" />
                    当前页:
<bean:write name="page" property="currentPage" />
                        跳转到:<html:select name="page" property="selectPage" onchange="pageNumber(this)">//这里获得页码,提交到Action
                               <html:optionsCollection name="page" property="pages" label="label" value="value"/>
                       </html:select>
                
</td>
            
</tr>
        
</table>

效果图:
posted on 2008-03-24 21:36 杨景 阅读(639) 评论(2)  编辑  收藏 所属分类: J2EE

Feedback

# re: Struts+Hibernate通用分页 2008-10-26 00:03 zhouyu
<logic:iterate name="page" property="list" id="YcAccident"
type="com.yls.car.bean.hibernate.YcAccident">
师哥,你看看你的这个property="list"属性,是不是应该写成property="resultList",要不然会报错的:No getter method for property: "list" of bean: "page"  回复  更多评论
  

# re: Struts+Hibernate通用分页[未登录] 2008-11-04 11:46 lz
hehe,谢谢楼上的帅哥,确实写错了  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: