传统的Spring和Hibernate分页,可能使访问不断继续,如果数据量太大,请求过程就会很大,笔者认为,如果改变思路,直接对查询后返回的结果进行分页,这样就不必每次进行页码变化时,都对数据库进行请求,效率大大提高!
但是同时相应的逻辑也变得稍微复杂了,只要把握好业务逻辑,就能进行快速开发!
以下是代码,和大家一起分享:
jsp页面:
<script type="text/javascript">
function pageFirst(count){
var currentPage = count;
if(currentPage >= 1)
{
document.getElementById("pagination").currentPage.value = 1;
document.getElementById("pagination").submit();
}
}
function pageFront(count){
var currentPage = count;
if(currentPage >=1)
{
document.getElementById("pagination").currentPage.value = count;
document.getElementById("pagination").submit();
}
else
{
return;
}
}
function pageNext(count){
var currentPage =count;
var totalPage =${totalPage};
if(currentPage < totalPage)
{
document.getElementById("pagination").currentPage.value =count;
document.getElementById("pagination").submit();
}
else
{
return;
}
}
function pageLast(count){
var currentPage = count;
var totalPage = ${totalPage};
if(currentPage <= totalPage)
{
document.getElementById("pagination").currentPage.value =totalPage;
document.getElementById("pagination").submit();
}
}
function toPage(count){
var currentPage = count;
var totalPage = ${totalPage};
if(currentPage > totalPage)
{
document.getElementById("pagination").currentPage.value=totalPage;
}
if(currentPage<1){
document.getElementById("pagination").currentPage.value=1;
}
document.getElementById("pagination").submit();
}
</script>
<html:form action="/dinner/findHotel.do?method=findMorePassagesAboutRes" method="post" styleId="pagination">
<html:hidden property="totalPage"/>
<html:hidden property="maxRecord"/>
<table width="95%" border="0" align="center" cellpadding="3"
cellspacing="0" class="table3">
<tr>
<td align="right" class="bianju_in">
<div class="body">
[
<a href="javascript:pageFirst(1);">第一页</a>] [
<a href="javascript:pageFront(${currentPage-1});">上一页</a>]|
[
<a href="javascript:pageNext(${currentPage+1});">下一页</a>]
[
<a href="javascript:pageLast(${totalPage});">最后一页</a>]
第
<input name="currentPage" id="currentPage" size="2" value="${currentPage}"/>页
<input type="hidden" name="maxRecord"
value="${maxRecord}" />
<input type="button" value=">>"
onClick="javascript:toPage(document.getElementById('currentPage').value)" />
共 ${totalPage} 页
</div>
</td>
</tr>
</table>
</html:form>
Action页面:
public ActionForward findList(ActionMapping mapping,ActionForm form,HttpservletRequest request,HttpServletresponse response){
TBRestaurantActionForm restaurantForm=(TBRestaurantActionForm)form;
request.setAttribute("recommend",dealWithLeaf(restaurantForm,recommend));
request.setAttribute("totalPage", restaurantForm.getTotalPage());
request.setAttribute("maxRecord",restaurantForm.getMaxRecord());
request.setAttribute("currentPage", restaurantForm.getCurrentPage());
}
public List dealWithLeaf(RestaurantActionForm form,List list){
RestaurantActionForm myform=(RestaurantActionForm)form;
long maxRecord=myform.getMaxRecord();
if(maxRecord%FETCHSIZE==0){ //刚好均匀分配
myform.setTotalPage((int)(maxRecord/FETCHSIZE));
return getFetchSizeByPageNo(myform.getCurrentPage(),list);
}
else{
myform.setTotalPage((int)(maxRecord/FETCHSIZE)+1);
List newlist=new ArrayList();
if(myform.getCurrentPage()<myform.getTotalPage()){
for (int i =(myform.getCurrentPage()-1)*FETCHSIZE; i <=myform.getCurrentPage()*FETCHSIZE-1 ; i++) {
newlist.add(list.get(i));
}
}
else{
for (int i = (myform.getCurrentPage()-1)*FETCHSIZE; i <maxRecord; i++) {
newlist.add(list.get(i));
}
}
return newlist;
}
}
public List getFetchSizeByPageNo(int currentPage,List list){
List newlist=new ArrayList();
for (int i =(currentPage-1)*FETCHSIZE; i <currentPage*FETCHSIZE-1; i++) {
newlist.add(i,list.get(i));
}
return newlist;
}
Dao页面:
public List<TBRestaurant> findTBRestaurantInfo(){
List<TBRestaurant> list=getHiberntateTemplate().find("from TBRestaurant");
return list;
}