在Web應用開發中,不管是有沒有數據庫,經常要用到分頁處理問題。EasyJWeb中通過引入IPageList接口來輕松解決我們的遇到的各種分頁問題,包括對數據庫記錄分頁、文件目錄分頁、數組或者Java對象分頁等。
EasyJWeb作為一個Web框架,其MVC核心中本身沒有包含分頁的內容,我們這里所說的分頁設計是指在EasyJWeb?Tools業務引擎中關于分頁需求應用的設計。
1、應用示例代碼 首先們看看該分頁設計的有關應用示例代碼,該示例的完整代碼可在
http://www.easyjf.com/download.htm中下載!
???
示例代碼A:com.easyjweb.action.userManageAction.java
這是EasyJWeb文檔中示例3(添刪改查)中有關記錄分頁顯示的部分代碼:
??public?class?userManageAction?extends?AbstractCrudAction?{
public?IPageList?doQuery(WebForm?form,?int?currentPage,?int?pageSize)?{
???....
????DbPageList?pList=new?DbPageList(User.class,scope,paras);//通過調用DbPageList對象,返回分頁結果IPageList
???????pList.doList(currentPage,pageSize);
???????return?pList;
??}
??...
從代碼中我們可以看出,這是一個對數庫記錄集對象的分頁處理例子。直接通實現了IPageList接口的DbPageList類實現數據庫的分頁。
示例代碼B:net.meybo.mail.action.EmailAction.java
這是MeyboMail?Web郵件客戶端開源簡化版中,中對郵件主題進行分頁顯示的代碼。
public?class?EmailAction?implements?IWebAction?{
...
private?Page?doList(WebForm?form,?Module?module,ActiveUser?user)
???{
...
List?list=null;
...
???list=EmailManage.getMailList(user.getUserName(),user.getServerDomain(),boxName);
IPageList?pList=new?PageList(new?ListQuery(list));??????
?????if(pList!=null){
???????????pList.doList(pageSize,currentPage,"","");
???????????form.addResult("list",pList.getResult());
???????????form.addResult("pages",new?Integer(pList.getPages()));
???????????form.addResult("rows",new?Integer(pList.getRowCount()));
???????????form.addResult("page",new?Integer(pList.getCurrentPage()));
???????????form.addResult("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages()));
???????????}??
...
上面例子中是對一個List集合進行分頁,因為MeyboMail?Web中沒有用到數據庫,所以使用ListQuery查詢處理器進行處理。
2、EasyJWeb?Tools中業務引擎中有關分頁的接口及類 EasyJWeb?Tools中通過使用IPageList及IQuery兩個接口對分頁問題進行抽象。
下面是IPageList接口的全部代碼:
????package?com.easyjf.web.tools;
???import?java.util.Collection;
???import?java.util.List;
???public?interface?IPageList?{
???public?List?getResult();//取回分頁的結果
???public?void?setQuery(IQuery?q);//設置查詢處理器
???public?int?getPages();//返回總頁數
???public?int?getRowCount();//返回總記錄數
???public?int?getCurrentPage();//返回當前頁
???public?void?doList(int?pageSize,int?pageNo,String?totalSQL,String?queryHQL);//執行分頁處理
???public?void?doList(int?pageSize,int?pageNo,String?totalSQL,String?queryHQL,Collection?paraValues);//執行分頁處理
???}
在IPageList中,我們看到通過設置查詢處理器實現數據的查詢及分頁,這里我們在看看IQuery接口的內容:
package?com.easyjf.web.tools;
import?java.util.Collection;
import?java.util.List;
public?interface?IQuery?{
int?getRows(String?conditing);//得到總記錄數
List?getResult(String?conditing);//根據條件查詢并返回結果
void?setFirstResult(int?begin);//設置開始記錄
void?setMaxResults(int?max);//設置每次查詢返回的最大記錄
void?setParaValues(Collection?paraValues);//設置查詢參數值
List?getResult(String?conditing,int?begin,int?max);//從結果集中begin開始的位置,取max條記錄
}
由此可見,我們的IPageList其實是通過設置調用不同的查詢處理器實現對不同類型數據來進行分頁處理的。?
3、通用分頁處理IPageList的實現PageList類 在EasyJWeb?Tools中,我們的PageList類實現了IPageList接口,其是一個通用的分頁處理類,其它各種類型數據的分頁可以通過繼承它來實現。
PageList.java的全部代碼如下:
package?com.easyjf.web.tools;
import?java.util.*;
/**
*?實現通過調用IQuery實現分頁處理
*?@author?蔡世友
*
*/
public?class?PageList??implements?IPageList{
private?int?rowCount;//記錄數
private?int?pages;//總頁數????
private?int?currentPage;//實際頁數
private?List?result;
private?IQuery?query;
public?PageList()
{
???
}
public?PageList(IQuery?q)
{
???this.query=q;
}
public?void?setQuery(IQuery?q)
{
???query=q;
}
public?List?getResult()
{
???return?result;
}
public?void?doList(int?pageSize,?int?pageNo,?String?totalSQL,?String?queryHQL)?{
???List?rs=null;????????
???int?total=query.getRows(totalSQL);
???if(total>0){????????????
???this.rowCount=total;
???this.pages=(this.rowCount?+?pageSize?-?1)?/?pageSize;?//記算總頁數???????
???int?intPageNo=(pageNo>this.pages?this.pages:pageNo);
???if(intPageNo<1)intPageNo=1;??????
???this.currentPage=intPageNo;
???if(pageSize>0){
???query.setFirstResult(?(intPageNo?-?1)?*?pageSize);???????
???query.setMaxResults(pageSize);
???}
???rs=query.getResult(queryHQL);???????
???}
???result=rs;
}
public?void?doList(int?pageSize,?int?pageNo,?String?totalSQL,?String?queryHQL,Collection?paraValues)?{
???List?rs=null;????
???query.setParaValues(paraValues);
???int?total=query.getRows(totalSQL);
???if(total>0){????????????
???this.rowCount=total;
???this.pages=(this.rowCount?+?pageSize?-?1)?/?pageSize;?//記算總頁數???????
???int?intPageNo=(pageNo>this.pages?this.pages:pageNo);
???if(intPageNo<1)intPageNo=1;??????
???this.currentPage=intPageNo;
???if(pageSize>0){
???query.setFirstResult(?(intPageNo?-?1)?*?pageSize);???????
???query.setMaxResults(pageSize);
???}
???rs=query.getResult(queryHQL);???????
???}
???result=rs;
}
public?int?getPages()?{
???return?pages;
}
public?int?getRowCount()?{
???return?rowCount;
}
public?int?getCurrentPage()?{
???return?currentPage;
}
}?
4、使用Hibernate訪問數據庫時的IQuery接口實現 這是EasyJF網站后臺中,使用Hibernate中間件對數據庫訪問時候,其對應查詢處理器IQuery接口的實現。
DbQuery.java的全部代碼:
package?com.easyjf.comm;
import?java.util.Collection;
import?java.util.List;
import?org.hibernate.Query;
import?org.hibernate.Session;
import?com.easyjf.web.tools.IQuery;
public?class?DbQuery?implements?IQuery?{????
???private?Session?session;????????
???private?int?begin;
???private?int?max;????
???private?List?paraValues;
???public?DbQuery(Session?session)
???{
???????this.session=session;????????
???}
???
???public?void?setParaValues(Collection?paraValues)?{????
???????this.paraValues=(List)paraValues;
???//????System.out.println("參數":paraValues.size();)
???}
???public?int?getRows(String?conditing)?{
???????Query?query1=session.createQuery(conditing);????????
???????if(paraValues!=null){????????
???????for(int?i=0;i<paraValues.size();i++)
???????{????????????
???????????query1.setParameter(i,paraValues.get(i));????????
???????????
???????}????????
???????}????????
???????int?total=((Integer)query1.uniqueResult()).intValue();????????
???????return?total;
???}
???public?List?getResult(String?conditing)?{????????
???????Query?query=session.createQuery(conditing);????
???????if(paraValues!=null){
???????????for(int?i=0;i<paraValues.size();i++)
???????????{
???????????????query.setParameter(i,paraValues.get(i));
???????????}????
???????????}
???????if(begin>0)query.setFirstResult(begin);
???????if(max>0)query.setMaxResults(max);
???????return?query.list();
???}
???public?void?setFirstResult(int?begin)?{
???????this.begin=begin;
???}
???public?void?setMaxResults(int?max)?{
???????this.max=max;
???}
???public?List?getResult(String?conditing,?int?begin,?int?max)?{
???????Query?query=session.createQuery(conditing);????
???????if(begin>0)query.setFirstResult(begin);
???????if(max>0)query.setMaxResults(max);
???????return?query.list();????????
???}
???public?void?setParaValues(List?paraValues)?{
???this.paraValues=paraValues;
???}
}?
5、對于List列表數據進行分頁的查詢處理器ListQuery實現 當我們要分頁的目標數據不是數據庫記錄,而是其它儲存方式時(如文本、xml或者內存中的實時對象線程等),可以轉化為數組或List等。這時可通過使用ListQuery查詢處理器實現分頁查詢。
ListQuery.java的全部代碼:
package?com.easyjf.web.tools;
import?java.util.ArrayList;
import?java.util.Collection;
import?java.util.List;
public?class?ListQuery?implements?IQuery?{
private?int?begin=0;
private?int?max=0;
private?List?list=null;
public?ListQuery()
{
????
}
public?ListQuery(List?l)
{????????
????if(l!=null){
????this.list=l;
????this.max=l.size();
????}
}
public?void?initList(List?l)
{
????this.list=l;
????this.max=l.size();
}
????public?int?getRows(String?conditing)?{
????????
????????return?(list==null?0:list.size());
????}
????public?List?getResult(String?conditing)?{????????
????????return?list.subList(begin,begin+max>list.size()?list.size():begin+max);
????}
????public?void?setFirstResult(int?begin)?{????????
????????this.begin=list.size()<begin?list.size():begin;????????
????}
????public?void?setMaxResults(int?max)?{
????????this.max=max;
????}
????public?List?getResult(String?conditing,?int?begin,?int?max)?{
????????
????????return?list;
????}????
????public?void?setParaValues(Collection?paraValues)?{
????????//?TODO?Auto-generated?method?stub????????
????}
}
6、關于分頁的算法討論 由于EasyJWeb?Tools業務引擎是對眾多分頁需求的簡單抽象。因此,無法對分頁查詢處理器的算法等作具體的限制,上面給出的兩個查詢處理器只是一個簡單的應用實例,大家可以根據實際應用項目中的需求進行查詢處理器的實現及算法設計。
由于水平有限,該設計上有很多不合理的地方,懇請大家指正!
7、EasyJWeb簡介 EasyJWeb是基于java技術,應用于WEB應用程序快速開發的MVC框架,框架設計構思來源于國內眾多項目實踐,充分借簽了當前主流的開源Web框架(Struts、JSF、Tapestry?、Webwork等),吸取了其優點及精華,利用Velocity作為模板頁面引擎,是一個實現了頁面及代碼完全分離的MVC開發框架,是一個旨在于為中小型Web應用系統提供快速開發實踐的簡易Web框架。
EasyJF開源團隊于2006年初才開始建設,因此當前整個開發團隊組建以及所發布的作品,都顯得極不成熟。EasyJWeb仍然處于測試階段,請廣大的Java愛好者多多批評及建議。同進也非常歡迎您能加入到我們的國產開源隊伍中。
EasyJWeb官方網址:
www.easyjf.com????EasyJF團隊官方網址:
www.easyjf.com?
posted on 2006-03-28 22:36
簡易java框架 閱讀(214)
評論(0) 編輯 收藏