做web時,經常要面對分頁這一問題,我也不知道究竟哪一種最好,就把想到的列出來:
第一種:先把所有數據都從服務器端獲取到客戶端,然后在web端進行分頁處理。但是對于一些很大數據量的列表,無疑效率很低的,用戶要等上好久才能見到效果(即使你用AJAX,它只能是用戶避免見到空白頁);但這種做法減少了和服務器的交互。
第二種:就是通過條件控制,web端僅獲取一頁的數據量,它提高了訪問服務器的效率,速度比較快,但是加大了訪問服務器的頻率。
我們用的比較多的是第二種。但還有一個問題,分頁的時候,要知道總共的頁數,有兩種做法:
第一種:在web端調用兩次服務端,一次獲取當前頁的數據,再一次獲取總共的頁數等信息。
第二種:在服務端包裝數據列表和總共的頁數信息,返回的僅是一個對象,然后到web端進行拆解。
比方說,用一個VO存放List和totalPage,可以把totalPage存放在另一個VO中,以便于以后的擴展。
不同的數據庫有不同的專業分頁語句。我們最常用的是oracle(參考hibernate):
public String getLimitString(String sql) {
StringBuffer pagingSelect = new StringBuffer(100);
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
pagingSelect.append(sql);
pagingSelect.append(" ) row_ where rownum <= ?) where rownum_ > ?");
return pagingSelect.toString();
}
MySql中的:
public String getLimitString(String sql) {
StringBuffer pagingSelect = new StringBuffer(100);
pagingSelect.append(sql);
pagingSelect.append(" limit ?, ?");
return pagingSelect.toString();
}
下面是jsp中的一些分頁代碼(采用第二種):
<script language="JavaScript">
//實現分頁功能 示例
// function forwardpage(page)
// {
// window.location="<!--%=request.getContextPath()%-->/.do?op=&cp="+page;// }
function selectPage(){
var thePage = document.all("spage").value;
if(thePage<1){
alert("請選擇一頁!");
}else{
turnToPage(thePage);
}
}
</script>
<%
String totalPage = (String) request.getAttribute("totalPage");
String currentNum = request.getParameter("cp");
int cp = 0;
int pageNum = 0;
if(totalPage==null||totalPage.equals("")){
totalPage = "10";
}
pageNum = Integer.parseInt(totalPage);
if(currentNum == null || currentNum.trim().equals("")){
cp = 1;
}else{
cp = Integer.parseInt(currentNum);
}
currentNum = null;
totalPage = null;
%>
<table width="100%" border="0">
<tr>
<td>共<%=pageNum%>頁</td>
<td></td>
<% if(cp == 1){ %>
<td width="30" align="center" align="center"><IMG alt=第一頁
src="<%=request.getContextPath()%/public/images/splitpage/unfirstpg.gif"></td>
<td width="20" align="center"><IMG
alt=上一頁 src="<%=request.getContextPath()%>/public/images/splitpage/unpreviouspg.gif">
</td>
<%
}
else
{
%>
<td width="20" align="center"><IMG alt=第一頁
src="<%=request.getContextPath()%>/public/images/splitpage/firstpg.gif" onclick="forwardpage('1')" style="cursor:hand"></td>
<td width="20" align="center"><IMG
alt=上一頁 src="<%=request.getContextPath()%>/public/images/splitpage/previouspg.gif" onclick="forwardpage('<%=cp-1%>')" style="cursor:hand">
</td>
<%
}
if(cp == pageNum){
%>
<td width="20" align="right"><IMG alt=下一頁
src="<%=request.getContextPath()%>/public/images/splitpage/unnextpg.gif">
</td>
<td width="20" align="right"><IMG alt=最后一頁
src="<%=request.getContextPath()%>/public/images/splitpage/unlastpg.gif"></td>
<% }
else{
%>
<td width="20" align="right"><IMG alt=下一頁
src="<%=request.getContextPath()%>/public/images/splitpage/nextpg.gif" onclick="forwardpage('<%=cp+1%>')" style="cursor:hand">
</td>
<td class="redbutton" width="20" align="right"><IMG alt=最后一頁
src="<%=request.getContextPath()%>/public/images/splitpage/lastpg.gif" onclick="forwardpage('<%=pageNum%>')" style="cursor:hand"></td>
<% } %>
<td width="20"> </td>
<td>跳轉到<select name="spage" onchange="selectPage()"><%
for(int pi=1;pi<=pageNum;pi++){
%>
<option value="<%=pi%>"><%=pi%></option>
<%
}
%></select>頁</td>
</tr>
</table>