王朝网络
分享
 
 
 

Spring+Hibernate+Struts技术的一个分页程序,共享给大家

王朝other·作者佚名  2007-02-07
宽屏版  字体: |||超大  

我用的是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,我觉得这样好像有些不太好,大家觉得怎样?大家提出宝贵的意见吧!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有