ITPUB论坛 » Java web开发及框架技术 » 实现可复用的分页组件(struts+spring+hibernate)
新一届的微软MVP评选已经开始,欢迎各位推荐!
2008-7-3 19:10 juanpeng
实现可复用的分页组件(struts+spring+hibernate)

Dao层:

首先定义一个公共的Dao借口:

Java代码
<SPAN style="COLOR: #000000">public interface CommonDao {   
    public List getUserByCutPage(final int startPage ,String tableName);   
    public Integer getRecordAmount(final String tableName);   
}</SPAN>  

public interface CommonDao {
        public List getUserByCutPage(final int startPage ,String tableName);
        public Integer getRecordAmount(final String tableName);
}



  




实现CommonDao

Java代码
<SPAN style="COLOR: #000000">public class CommonDaoImp extends HibernateDaoSupport implements CommonDao{   
      public List getUserByCutPage(final int startResult,final String tableName)   
      {   
          return this.getSessionFactory().getCurrentSession().createQuery(tableName)   
                .setFirstResult(startResult)   
                .setMaxResults(CutPage.preferSize)   
                .list();   
      }   
  
    public Integer getRecordAmount(final String tableName) {   
        // TODO Auto-generated method stub   
         return (Integer)this.getHibernateTemplate().execute(new HibernateCallback()   
         {   
          public Object doInHibernate(Session session)   
             {   
                return (Integer)session.createQuery("select count(*)"+</SPAN>   
  
  
  
<SPAN style="COLOR: #000000">tableName</SPAN>   
  
  
  
  
).uniqueResult();   
             }   
          });   
           
    }      
}  

public class CommonDaoImp extends HibernateDaoSupport implements CommonDao{
          public List getUserByCutPage(final int startResult,final String tableName)
          {
                  return this.getSessionFactory().getCurrentSession().createQuery(tableName)
                                  .setFirstResult(startResult)
                                  .setMaxResults(CutPage.preferSize)
                                  .list();
      }

        public Integer getRecordAmount(final String tableName) {
                // TODO Auto-generated method stub
                 return (Integer)this.getHibernateTemplate().execute(new HibernateCallback()
         {
                 public Object doInHibernate(Session session)
             {
                                 return (Integer)session.createQuery("select count(*)"+



tableName




).uniqueResult();
             }
          });
               
        }   
}  

然后由所有的dao引用或者继承CommonDao,并且在其中注如入tableName参数







Service层:




Java代码
<SPAN style="COLOR: #000000">public int getCutPage() {   
        // TODO Auto-generated method stub   
        return this.customerDao.getRecordCount();   
    }</SPAN>  

public int getCutPage() {
                // TODO Auto-generated method stub
                return this.customerDao.getRecordCount();
        }



  

Action层:




Java代码
<SPAN style="COLOR: #000000">public ActionForward execute(ActionMapping mapping, ActionForm form,   
            HttpServletRequest request, HttpServletResponse response) {   
        // TODO Auto-generated method stubz   
        </SPAN>   
  
  
  
<SPAN style="COLOR: #000000">CutPage cutPage=new CutPage();</SPAN>   
  
  
  
  
  
  
  
        <SPAN style="COLOR: #000000">cutPage.setPageIndex(request.getParameter("pageIndex"));   
        cutPage.setRecordCount(this.customerService.getCutPage());</SPAN>   
  
  
  
  
  
  
  
        request.setAttribute("cutPage", cutPage);   
        return mapping.findForward(result);   
      
    }  

public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {
                // TODO Auto-generated method stubz
               



CutPage cutPage=new CutPage();







                cutPage.setPageIndex(request.getParameter("pageIndex"));
                cutPage.setRecordCount(this.customerService.getCutPage());







                request.setAttribute("cutPage", cutPage);
                return mapping.findForward(result);
       
        }


CutPage类:




Java代码
<SPAN style="COLOR: #000000">public class CutPage {   
    public final static int preferSize=5;   
    private Integer pageIndex=new Integer(1);   
    private int startRecord;   
    private int recordCount;   
    private int pageCount;   
  
    public int getPageIndex() {   
        return pageIndex.intValue();   
    }   
  
    public void setPageIndex(String pageIndex) {   
        if(pageIndex!=null)   
        {   
            this.pageIndex = new Integer(pageIndex);   
        }   
    }   
      
    public int getRecordCount()   
    {   
        return recordCount;   
    }   
      
    public void setRecordCount(int recordCount)   
    {   
        this.recordCount=recordCount;   
    }   
      
    public int getPageCount()   
    {   
        pageCount=this.getRecordCount()/this.preferSize;   
        int temCount=this.getRecordCount()%this.preferSize;   
        if(temCount!=0)   
        {   
            pageCount++;   
        }   
        return pageCount;   
    }   
  
    public int getStartRecord() {   
        return (this.getPageIndex()-1)*this.preferSize;   
    }   
  
    public void setStartRecord(int startRecord) {   
        this.startRecord = startRecord;   
    }   
}   
</SPAN>  

public class CutPage {
        public final static int preferSize=5;
        private Integer pageIndex=new Integer(1);
        private int startRecord;
        private int recordCount;
        private int pageCount;

        public int getPageIndex() {
                return pageIndex.intValue();
        }

        public void setPageIndex(String pageIndex) {
                if(pageIndex!=null)
                {
                        this.pageIndex = new Integer(pageIndex);
                }
        }
       
        public int getRecordCount()
        {
                return recordCount;
        }
       
        public void setRecordCount(int recordCount)
        {
                this.recordCount=recordCount;
        }
       
        public int getPageCount()
        {
                pageCount=this.getRecordCount()/this.preferSize;
                int temCount=this.getRecordCount()%this.preferSize;
                if(temCount!=0)
                {
                        pageCount++;
                }
                return pageCount;
        }

        public int getStartRecord() {
                return (this.getPageIndex()-1)*this.preferSize;
        }

        public void setStartRecord(int startRecord) {
                this.startRecord = startRecord;
        }
}




最后,只要做一个专门的jsp页面面,只要需要时用




Java代码
<SPAN style="COLOR: #000000"><jsp:include page="cutpage.jsp">   
        <jsp:param name="</SPAN>   
  
  
  
<SPAN style="COLOR: #000000">hrefName </SPAN>   
  
  
  
  
  
" value="<SPAN style="COLOR: #000000">selectItem</SPAN>   
  
  
  
  
  
"/>   
</jsp:include>  

<jsp:include page="cutpage.jsp">
            <jsp:param name="



hrefName





" value="selectItem





"/>
</jsp:include>cutpage.jsp:




Java代码
<SPAN style="COLOR: #000000">  <body>   
    <%   
        String hrefName=request.getParameter("hrefName");   
        CutPage cutPage=(CutPage)request.getAttribute("cutPage");   
        int pageCount=cutPage.getPageCount();   
        for(int i=0;i<pageCount;i++)   
        {   
            int k=1;   
            out.println("<a href="+</SPAN>   
  
  
  
<SPAN style="COLOR: #000000">hrefName</SPAN>   
  
  
  
  
+".do?pageIndex="+(k+i)+">"+(k+i)+"</a>");   
        }   
     %>   
      
  </body>

2008-7-3 19:11 smartpig
这个代码 怎么这么多html脚本?楼主打个包吧

2008-7-3 19:12 hanfeishu
你不觉得麻烦吗?那么多的java代码,为什么不统一写在一处,分类呢,

2008-7-3 19:12 cyntha80
我想说的是......robbin曾经把他的分页组件贴出来过。。

另外lz的封装实在不敢恭维。。。太多情况没考虑到。

2008-7-3 22:39 xieye
看的累~~

2008-7-4 17:01 ericlntm
分页问题有这么难吗?怎么写这么多代码?一个javaBean就搞好了。然后传给jsp就可以了吧。

2008-7-4 18:06 justforregister
就是

页: [1]


Powered by ITPUB论坛