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>