论坛首页 Java版 Spring

实现可复用的分页组件(struts+spring+hibernate)

浏览 2397 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-06-09

Dao层:

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

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



 


实现CommonDao

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层:


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



 

 Action层:


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类:


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页面面,只要需要时用


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



hrefName 





" value="selectItem





"/>
</jsp:include>

cutpage.jsp:


  <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="+



hrefName




+".do?pageIndex="+(k+i)+">"+(k+i)+"</a>");
  		}
  	 %>
   
  </body>

 


   
最后更新时间:2008-06-10
这个代码 怎么这么多html脚本?楼主打个包吧
   
0 请登录后投票
最后更新时间:2008-06-10
你不觉得麻烦吗?那么多的java代码,为什么不统一写在一处,分类呢,
   
0 请登录后投票
最后更新时间:2008-06-10
如果有查询怎么办?
   
0 请登录后投票
最后更新时间:2008-06-10
godson_2003 写道
这个代码 怎么这么多html脚本?楼主打个包吧

由于是第一次在JavaEye上发帖子,好多功能还不会用,在写代码的时候选择了一下字体颜色,之后就调不回去了,给大家阅读时带来的不便,还请谅解
   
0 请登录后投票
最后更新时间:2008-06-12
我想说的是......robbin曾经把他的分页组件贴出来过。。

另外lz的封装实在不敢恭维。。。太多情况没考虑到。
   
0 请登录后投票
最后更新时间:2008-06-15
EXvision 写道
我想说的是......robbin曾经把他的分页组件贴出来过。。

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

有什么情况没考虑到啊?还请指点
   
0 请登录后投票
最后更新时间:2008-06-15
查询也能在Service层构造,然后传给CommonDao啊
   
0 请登录后投票
最后更新时间:2008-07-03
直接用一些网上开源的组件比较好

像 pagetag valuelist dispalytag extreme ecside


只需要你构造一个list 返回就可以
   
0 请登录后投票
最后更新时间:2008-07-07
楼上你错了吧,貌似这些框架都是内存分页的
   
0 请登录后投票
论坛首页 Java版 Spring

跳转论坛:
JavaEye推荐