版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
HYPERLINK”http://blog。chinauni/uid-332473—id-2132130.html”Elicpse+Struts實現(xiàn)數(shù)據(jù)分頁顯示2006—07—1111:23:16分類:Java一開發(fā)環(huán)境Elicpse+StrutsStudio+SqlServer2000+Tomcat(yī)。二開發(fā)思路既然講的是Struts,那自然離不了MVC,分頁顯示也是如此.1建立適當(dāng)?shù)哪P徒M件,對應(yīng)你要查詢數(shù)據(jù)庫中的表。這部分由我們熟識的javaBean來充當(dāng)。并在其中建立數(shù)據(jù)庫查詢方法,該方法需要一個java.sql。Conntection類型的參數(shù),并返回一個ArrayList。在本例中為Book.java2建立分頁所需要的模型組件,也是由javaBean來充當(dāng),通過由Book中供應(yīng)的ArrayList來構(gòu)造。本例中為PageBean。java.。3建立掌握器組件,這部分由Struts中的Action來實現(xiàn).主要負(fù)責(zé)將實例化Book,并利用返回的ArrayList對象,構(gòu)造PageBean。以及接收由視圖傳遞而來的action參數(shù).從而在PageBean對象中調(diào)用不同的方法,該方法返回Book[]對象.最后將Book[]和PageBean放入request中。本例中為PageListAction.java.4建立視圖組件,這部分由jsp來充當(dāng),為了不消滅java代碼,我們使用Struts供應(yīng)的標(biāo)簽庫,主要負(fù)責(zé)從request中取出剛剛放入的對象,通過反復(fù)調(diào)用PageListAction以及action參數(shù),而實現(xiàn)分頁顯示。本例中為pagetest.jsp。5建立并配置struts-config。xml。6建立數(shù)據(jù)庫。三實例代碼??1Book.javapackagebean;?importjava。sql。*;?importjava。util。ArrayList;?/**?*@作者吳步強(qiáng)?*Struts分頁顯示數(shù)據(jù)Bean,對應(yīng)數(shù)據(jù)庫中Book表?*/?publicclassBook{
privateStringbookname;//書名?privateStringauthor;//作者?privateStringprice;//價格??publicBook(Stringname,Stringauthor,Stringprice){?this.bookname=name;?this。author=author;?this.price=price;?}?
publicStringgetAuthor(){
returnauthor;
}??publicvoidsetAuthor(Stringauthor){?this.a(chǎn)uthor=author;
}??publicStringgetBookname(){?returnbookname;?}??publicvoidsetBookname(Stringbookname){?this.bookname=bookname;
}
?publicStringgetPrice(){
returnthis.price;
}??publicvoidsetPrice(Stringprice){?this.price=price;?}??publicstaticArrayListgetAllBook(Connectionconnection){?Stringsql="select*frombook";?ArrayListarrayList=newArrayList();?try{?Statementstatement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);?ResultSetresultSet=statement。executeQuery(sql);?System.out.println(”BookBean數(shù)據(jù)查詢已完成!");
while(resultSet.next())
{?Stringname=resultSet。getString("name”);
Stringauthor=resultSet.getString(”author");?Stringprice=resultSet.getString("price”);?System。out。println("開頭數(shù)據(jù)封裝:name="+name+"author=”+author+"price="+price);?Bookbook=newBook(name,author,price);?arrayList.a(chǎn)dd(book);
}?connection.close();?resultSet。close();?}cat(yī)ch(SQLExceptione)?{?System。out。println(”數(shù)據(jù)庫特別”+e。toString());?}returnarrayList;?}
}2PageBean.java?
packagepage;?importbean.Book;
importjava。util.*;
/**
*@作者吳步強(qiáng)?*Struts分頁顯示規(guī)律Bean
*/?publicclassPageBean{
?intcurrentPage=1;//當(dāng)前頁?publicinttotalPages=0;//總頁數(shù)?intpageRecorders=5;//每頁5條數(shù)據(jù)?inttotalRows=0;//總數(shù)據(jù)數(shù)?intpageStartRow=0;//每頁的起始數(shù)?intpageEndRow=0;//每頁顯示數(shù)據(jù)的終止數(shù)?booleanhasNextPage=false;//是否有下一頁
booleanhasPreviousPage=false;//是否有前一頁?ArrayListarrayList;?Iteratorit;?publicPageBean(){}??publicPageBean(ArrayListarrayList){?this.arrayList=arrayList;
totalRows=arrayList.size();?it=arrayList.iterator();?hasPreviousPage=false;
currentPage=1;
if((totalRows%pageRecorders)==0)?{
totalPages=totalRows/pageRecorders;?}?else?{?totalPages=totalRows/pageRecorders+1;
}??if(currentPage>=totalPages)?{?hasNextPage=false;
}?else?{?hasNextPage=true;?}?if(totalRows<pageRecorders)?{?this.pageStartRow=0;?this。pageEndRow=totalRows;?}?else?{?this。pageStartRow=0;?this.pageEndRow=pageRecorders;
}}/**?*@returnReturnsthecurrentPage.?*/?publicStringgetCurrentPage(){?returnthis.toString(currentPage);?}?/**?*@paramcurrentPageThecurrentPagetoset。?*/?publicvoidsetCurrentPage(intcurrentPage){
this.currentPage=currentPage;?}
/**?*@returnReturnsthepageRecorders。
*/
publicintgetPageRecorders(){?returnpageRecorders;?}?/**
*@parampageRecordersThepageRecorderstoset.?*/?publicvoidsetPageRecorders(intpageRecorders){?this.pageRecorders=pageRecorders;?}?/**?*@returnReturnsthepageEndRow.
*/
publicintgetPageEndRow(){?returnpageEndRow;?}?/**?*@returnReturnsthepageStartRow。
*/?publicintgetPageStartRow(){?returnpageStartRow;?}
/**?*@returnReturnsthetotalPages。
*/?publicStringgetTotalPages(){??returnthis.toString(totalPages);?}?/**?*@returnReturnsthetotalRows.?*/?publicStringgetTotalRows(){
returnthis.toString(totalRows);?}?/**?*@returnReturnsthehasNextPage.?*/?publicbooleanisHasNextPage(){?returnhasNextPage;?}?/**?*@paramhasNextPageThehasNextPagetoset。?*/?publicvoidsetHasNextPage(booleanhasNextPage){?this.hasNextPage=hasNextPage;?}?/**?*@returnReturnsthehasPreviousPage.?*/
publicbooleanisHasPreviousPage(){?returnhasPreviousPage;?}?/**?*@paramhasPreviousPageThehasPreviousPagetoset.
*/?publicvoidsetHasPreviousPage(booleanhasPreviousPage){?this.hasPreviousPage=hasPreviousPage;?}?publicBook[]getNextPage(){
?currentPage=currentPage+1;?System。out。println("PageBean.getNextPage()正在執(zhí)行;");?System.out。println(”參數(shù)currentPage="+currentPage);??if((currentPage-1)>0)
{
hasPreviousPage=true;?}?else?{
hasPreviousPage=false;
}
?if(currentPage〉=totalPages)?{
hasNextPage=false;?}?else?{?hasNextPage=true;?}?System。out。println("參數(shù)hasNextPage="+hasNextPage);?System.out.println("籌備執(zhí)行PageBean.getBooks()");?Book[]books=getBooks();?this.description();
?returnbooks;
}?
publicBook[]getPreviouspage(){
?currentPage=currentPage-1;if(currentPage==0){currentPage=1;}
if(currentPage〉=totalPages)?{?hasNextPage=false;?}?else?{?hasNextPage=true;?}?if((currentPage-1)>0)?{
hasPreviousPage=true;?}
else?{?hasPreviousPage=false;?}?Book[]books=getBooks();?this。description();?returnbooks;?}publicBook[]getBooks(){?System.out.println("pageBean。getBooks()開頭執(zhí)行;");???if(currentPage*pageRecorders〈totalRows){//推斷是否為最后一頁?pageEndRow=currentPage*pageRecorders;?pageStartRow=pageEndRow—pageRecorders;?}?else{
pageEndRow=totalRows;
pageStartRow=pageRecorders*(totalPages-1);?}?Book[]books=newBook[pageEndRowStartRow+1];??System。out.println("pageStartRow=”+pageStartRow);?System。out.println("pageEndRow="+pageEndRow);?intj=0;
for(inti=pageStartRow;i<pageEndRow;i++)?{?
Bookbook=(Book)arrayList.get(i);?books[j++]=book;?
}?System。out.println("要顯示的頁面數(shù)據(jù)已經(jīng)封裝,簡略信息如下:");?this.description();?returnbooks;
}?
publicStringtoString(inttemp)?{?Stringstr=Integer。toString(temp);?returnstr;
}?
publicvoiddescription()
{??Stringdescription=”共有數(shù)據(jù)數(shù):”+this。getTotalRows()+"共有頁數(shù):"+this.getTotalPages()+”當(dāng)前頁數(shù)為:"+this.getCurrentPage()+??"是否有前一頁:”+this.isHasPreviousPage()+"是否有下一頁:”+this。isHasNextPage()+"開頭行數(shù):"+this.getPageStartRow()+”終止行數(shù):"+this.getPageEndRow();System.out.println(description);}?}3PageListAction。java??packagepage;?importorg。apache.struts.action。*;
importjavax.servlet。http.*;
importcomm.Constants;
?importbean.Book;?importjava.util.*;
importjavax。sql.DataSource;?/**?*@author吳步強(qiáng)?*Struts分頁顯示Action?*/?publicclassPageListActionextendsAction{?
publicPageListAction(){}?ArrayListarrayList=newArrayList();?PageBeanpb;??publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{?Stringaction;?action=request.getParameter(”action");
if(action==null||action.equals("null”)){//第一次讀取數(shù)據(jù)?try{?DataSourcedatasource=this.getDataSource(request,Constants.DATASOURCE_KEY);?arrayList=Book.getAllBook(dat(yī)asource.getConnection());?System。out.println("第一步,數(shù)據(jù)已經(jīng)成功傳遞到Action,action="+action);?}catch(Exceptione){?e.printStackTrace();?System.out.println("數(shù)據(jù)庫連接消滅特別");
}??pb=newPageBean(arrayList);?Book[]books=pb.getBooks();?pb.description();?request。setAttribute("result",books);?request。setAttribute("page",pb);??}
else
{?if(action=="nextPage”||action.equals("nextPage”))?{
System.out.println("參數(shù)action=”+action);?System.out.println(”函數(shù)pb。getNextPage()籌備執(zhí)行”);?Book[]books=pb.getNextPage();
request.setAttribute("page",pb);?request.setAttribute("result",books);?}?if(action==”previousPage"||action.equals("previousPage”))?{?System。out.println("參數(shù)action="+action);?System.out.println("函數(shù)pb.getPreviouspage()籌備執(zhí)行”);?Book[]books=pb。getPreviouspage();?request.setAttribute("page",pb);?request.setAttribute("result",books);??}?}
return(mapping.findForward(”success"));?}?}?4pagetest。jsp??<%@tagliburi="/WEB—INF/struts-logic.tld”prefix=”logic"%>?〈%@tagliburi=”/WEB-INF/struts—bean.tld"prefix="bean"%>
<%@tagliburi="/WEB-INF/struts—html。tld"prefix=”html”%〉
<%@pagecontentType="text/html;charset=gb2312"language=”java"%>??〈html:htmllocale="true”>?<head>?<metahttp-equiv="Content-Type"content="text/html;charset=gb2312”>
〈/head>?<body〉??<tableborder="1"〉
<tr〉<th>書名</th><th>作者〈/th>〈th>價格</th></tr〉?<logic:presentname="result”〉?〈logic:iterateid=”book"name="result”type=”bean.Book">?〈logic:presentname="book">?〈tr〉?<td><bean:writename="book”property=”bookname"/></td>
<td><bean:writename="book"property=”author"/>〈/td〉
<td><bean:writename="book"property="price”/></td〉?</tr>?</logic:present>?〈/logic:iterate>
</logic:present>?</table>?<logic:equalname="page"property=”hasNextPage”value="true">?〈html:linkpage="/page。do?action=nextPage”>nextPage〈/html:link>
〈/logic:equal>?<logic:equalname=”page”property="hasPreviousPage”value="true">
<html:linkpage="/page.do?action=previousPage">PreviousPage〈/html:link>?</logic:equal〉?共有數(shù)據(jù)總數(shù)<bean:writename=”page”property="totalRows"/〉;
共分<bean:writename=”page”property="totalPages"/>頁,當(dāng)前是第
〈bean:writename=”page"property=”currentPage”/>頁?〈/body>?〈/html:html>?
5struts-config。xml?
<?xmlversion="1.0”encoding="ISO-8859-1"?>??<?。模螩TYPEstruts-configPUBLIC
"—//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN”?”http://jakarta。apache.org/struts/dtds/struts—config_1_1。dtd";><struts—config>
<dat(yī)a-sources>?<data—sourcekey="dataSource"type=”org.apache.commons.dbcp。BasicDat(yī)aSource"〉
<set-propertyproperty=”driverClassName"value="com.microsoft.jdbc.sqlserver。SQLServerDriver"/〉?<set-propertyproperty="url"value=”jdbc
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 病害橋梁處置方案
- 班級鑒定機(jī)構(gòu)裝修方案
- 班級主題活動課程設(shè)計
- 2024年汽油越野車項目提案報告范文
- 玻璃隔斷施工安全方案
- 玻璃覆蓋工藝設(shè)計研究報告
- 玻璃受熱炸裂機(jī)理研究報告
- 玻璃企業(yè)用煤管理方案
- 爆破工程隧道開挖課程設(shè)計
- 濱湖隔音屏施工方案
- 管理能力與領(lǐng)導(dǎo)力管理培訓(xùn)
- 2023上半年四川公務(wù)員考試申論試題(省市卷)
- 2024年度專業(yè)會務(wù)組織服務(wù)協(xié)議書版
- 函數(shù)的圖象及變換省公開課獲獎?wù)n件說課比賽一等獎?wù)n件
- 2020-2021學(xué)年河南省洛陽市高一上學(xué)期期中考試化學(xué)試題
- 四年級上冊語文第六單元任務(wù)群教學(xué)設(shè)計
- 2024-2025學(xué)年北師大版九年級數(shù)學(xué)上冊期中培優(yōu)試題
- 《高血壓科普知識》課件
- 《建筑工程設(shè)計文件編制深度規(guī)定》(2022年版)
- 心理咨詢中知情同意的倫理困境與解決途徑
- 山地光伏除草施工方案
評論
0/150
提交評論