| 導購 | 订阅 | 在线投稿
分享
 
 
 

Spring+Hibernate+Struts技術的一個分頁程序,共享給大家

來源:互聯網  2007-02-07 12:18:45  評論

我用的是Spring+Hibernate+Struts框架:

這是我的DAO的核心代碼

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

// 用Spring支持的Hibernate方法,使Hibernate對數據庫的操作繼續瘦身

public List getOfficeBySearchCriteria(final String hsql,final int pageNo,final int page_size) throws DataAccessException // hsql 是如:"select office1 from Office as office1 order by office1.officename";pageNo 是第幾頁;page_size是每頁記錄數

{

String sql;

int total_count=0;

List offices=new ArrayList();

//offices= getHibernateTemplate().find("from Office office1 where office1.officename like ?", "%"+officeName+"%");

offices= getHibernateTemplate().find(hsql); //爲了得到總記錄數

total_count=offices.size();

crossPageInfo= crossPageBean.getCrossPageInfo(total_count,pageNo,page_size);

sql=hsql+ " limit " + (pageNo-1)*page_size + "," +page_size;

offices= getHibernateTemplate().find(sql); //爲了得到頁記錄信息 System.out.println("The list offices size: "+offices.size());

return offices;

}

//其中crossPageBean.getCrossPageInfo只是得到頁面的如:總頁數、供多少頁的信息等一般的翻頁信息;

我在Action中是這樣調用的:

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception

{

CrossPageInfo crossPageInfo=new CrossPageInfo();

String hsql="select office1 from Office office1 order by office1.officename";

String pageNo=request.getParameter("pageNo");

int pageNoi=1;

if(pageNo==null)

pageNo="1";

pageNoi=Integer.parseInt(pageNo);

int pageSize=5;

//List offices=getOfficeService().getAllOffice();

List offices=getOfficeService().getOfficeBySearchCriteria(hsql,pageNoi,pageSize);

crossPageInfo=getOfficeService().getCrossPageInfo();

System.out.println("The CorssPgaeInfo :"+crossPageInfo.getPageNo());

System.out.println(crossPageInfo.getPageSize());

request.setAttribute("offices",offices);

request.setAttribute("pageInfo",crossPageInfo);

return mapping.findForward("success");

//throw new UnsupportedOperationException("Generated method 'execute(...)' not implemented.");

}

//其中getOfficeService()只是提供接口服務的方法。

我的表現頁面是這樣的:

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>

<%@ page import="com.uplus.util.CrossPageInfo"%>

<html>

<head>

<title>

mySearchCList

</title>

</head>

<body bgcolor="#ffffff">

<form name="form1" action="officesearch.do" method="post">

<table >

<tr>

<td>OfficeName:<input name="officeName" type="text"></td><td><input type="submit" name="sb" value="Search"></td>

</tr>

</table>

</form>

<br><a href="/jsp/office/officeadd.jsp">Add</a>

<table bgcolor="#DBE9F1" align="center" class="InputFrameMain" style="MARGIN: 0px" cellSpacing="1" cellPadding="0" BGALIGN="CENTER" BGVALIGn="middle" width="100%" VALIGN="middle" >

<tr><td align="center">OfficeName</td><td align="center">OfficePhone</td></tr>

<logic:iterate id="office" name="offices" >

<tr bgcolor="#ffffff">

<td align="center"><a href="officesee.do?id=<bean:write name='office' property='id'/>" target="_blank"><bean:write name="office" property="officename"/></a></td>

<td align="center"><bean:write name="office" property="officephone"/></td>

<td align="center"><a href="officeedit.do?id=<bean:write name='office' property='id'/>" >Update </a>

<td align="center"><a href="officedel.do?id=<bean:write name='office' property='id'/>" onclick="return confirm('Would You Detele It? ')" >Delete </a>

</tr>

</logic:iterate>

</table>

<%CrossPageInfo cpInfo=(CrossPageInfo)request.getAttribute("pageInfo");%>

<table width="100%" align="center" class="InputFrameMain" style="MARGIN: 0px" cellPadding="0" cellSpacing="0">

<tr ><form action="officelist.do" method="post" onsubmit='return checkform2(this)'>

<td width=70%>Total <font color="blue"><%=cpInfo.getTotalRow()%></font>&items found,Total&<font color="blue"><%=cpInfo.getTotalPage()%></font> Pages,Current No <font color="blue"><%=cpInfo.getPageNo()%> </font>Page.

Go to <input name="pageNo" type="text" size="5" class="input">Page

<input name="sb2" type="submit" class="button" value="Go">

</td></form>

<td width=30% align='left'>

<%if(cpInfo.getPageNo()>1){%>

&<a href="officelist.do?pageNo=1">

<%}%>First</a>

<%if(cpInfo.getPageNo()>1){ %>

&<a href="officelist.do?pageNo=<%=cpInfo.getPageNo()-1%>">

<%}%>Previous</a>

<%if(cpInfo.getPageNo()<cpInfo.getTotalPage()){ %>

&<a href="officelist.do?pageNo=<%=cpInfo.getPageNo()+1%>">

<%}%>Next</a>

<%if(cpInfo.getTotalPage()>cpInfo.getPageNo()){%>

&<a href="officelist.do?pageNo=<%=cpInfo.getTotalPage()%>">

<%}%>Last</a></td>

</tr>

</table>

</body>

</html>

大家可以看一下我的處理過程,其中在DAO裏爲了得到總計錄數執行了一次次數據表查詢HSQL;得到數據又執行了一次HSQL,我覺得這樣好像有些不太好,大家覺得怎樣?大家提出寶貴的意見吧!

我用的是Spring+Hibernate+Struts框架: 這是我的DAO的核心代碼 import org.springframework.orm.hibernate.support.HibernateDaoSupport; // 用Spring支持的Hibernate方法,使Hibernate對數據庫的操作繼續瘦身 public List getOfficeBySearchCriteria(final String hsql,final int pageNo,final int page_size) throws DataAccessException // hsql 是如:"select office1 from Office as office1 order by office1.officename";pageNo 是第幾頁;page_size是每頁記錄數 { String sql; int total_count=0; List offices=new ArrayList(); //offices= getHibernateTemplate().find("from Office office1 where office1.officename like ?", "%"+officeName+"%"); offices= getHibernateTemplate().find(hsql); //爲了得到總記錄數 total_count=offices.size(); crossPageInfo= crossPageBean.getCrossPageInfo(total_count,pageNo,page_size); sql=hsql+ " limit " + (pageNo-1)*page_size + "," +page_size; offices= getHibernateTemplate().find(sql); //爲了得到頁記錄信息 System.out.println("The list offices size: "+offices.size()); return offices; } //其中crossPageBean.getCrossPageInfo只是得到頁面的如:總頁數、供多少頁的信息等一般的翻頁信息; 我在Action中是這樣調用的: public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CrossPageInfo crossPageInfo=new CrossPageInfo(); String hsql="select office1 from Office office1 order by office1.officename"; String pageNo=request.getParameter("pageNo"); int pageNoi=1; if(pageNo==null) pageNo="1"; pageNoi=Integer.parseInt(pageNo); int pageSize=5; //List offices=getOfficeService().getAllOffice(); List offices=getOfficeService().getOfficeBySearchCriteria(hsql,pageNoi,pageSize); crossPageInfo=getOfficeService().getCrossPageInfo(); System.out.println("The CorssPgaeInfo :"+crossPageInfo.getPageNo()); System.out.println(crossPageInfo.getPageSize()); request.setAttribute("offices",offices); request.setAttribute("pageInfo",crossPageInfo); return mapping.findForward("success"); //throw new UnsupportedOperationException("Generated method 'execute(...)' not implemented."); } //其中getOfficeService()只是提供接口服務的方法。 我的表現頁面是這樣的: <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %> <%@ page import="com.uplus.util.CrossPageInfo"%> <html> <head> <title> mySearchCList </title> </head> <body bgcolor="#ffffff"> <form name="form1" action="officesearch.do" method="post"> <table > <tr> <td>OfficeName:<input name="officeName" type="text"></td><td><input type="submit" name="sb" value="Search"></td> </tr> </table> </form> <br><a href="/jsp/office/officeadd.jsp">Add</a> <table bgcolor="#DBE9F1" align="center" class="InputFrameMain" style="MARGIN: 0px" cellSpacing="1" cellPadding="0" BGALIGN="CENTER" BGVALIGn="middle" width="100%" VALIGN="middle" > <tr><td align="center">OfficeName</td><td align="center">OfficePhone</td></tr> <logic:iterate id="office" name="offices" > <tr bgcolor="#ffffff"> <td align="center"><a href="officesee.do?id=<bean:write name='office' property='id'/>" target="_blank"><bean:write name="office" property="officename"/></a></td> <td align="center"><bean:write name="office" property="officephone"/></td> <td align="center"><a href="officeedit.do?id=<bean:write name='office' property='id'/>" >Update </a> <td align="center"><a href="officedel.do?id=<bean:write name='office' property='id'/>" onclick="return confirm('Would You Detele It? ')" >Delete </a> </tr> </logic:iterate> </table> <%CrossPageInfo cpInfo=(CrossPageInfo)request.getAttribute("pageInfo");%> <table width="100%" align="center" class="InputFrameMain" style="MARGIN: 0px" cellPadding="0" cellSpacing="0"> <tr ><form action="officelist.do" method="post" onsubmit='return checkform2(this)'> <td width=70%>Total <font color="blue"><%=cpInfo.getTotalRow()%></font>&items found,Total&<font color="blue"><%=cpInfo.getTotalPage()%></font> Pages,Current No <font color="blue"><%=cpInfo.getPageNo()%> </font>Page. Go to <input name="pageNo" type="text" size="5" class="input">Page <input name="sb2" type="submit" class="button" value="Go"> </td></form> <td width=30% align='left'> <%if(cpInfo.getPageNo()>1){%> &<a href="officelist.do?pageNo=1"> <%}%>First</a> <%if(cpInfo.getPageNo()>1){ %> &<a href="officelist.do?pageNo=<%=cpInfo.getPageNo()-1%>"> <%}%>Previous</a> <%if(cpInfo.getPageNo()<cpInfo.getTotalPage()){ %> &<a href="officelist.do?pageNo=<%=cpInfo.getPageNo()+1%>"> <%}%>Next</a> <%if(cpInfo.getTotalPage()>cpInfo.getPageNo()){%> &<a href="officelist.do?pageNo=<%=cpInfo.getTotalPage()%>"> <%}%>Last</a></td> </tr> </table> </body> </html> 大家可以看一下我的處理過程,其中在DAO裏爲了得到總計錄數執行了一次次數據表查詢HSQL;得到數據又執行了一次HSQL,我覺得這樣好像有些不太好,大家覺得怎樣?大家提出寶貴的意見吧!
󰈣󰈤
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有