struts实现分页显示_第1页
struts实现分页显示_第2页
struts实现分页显示_第3页
struts实现分页显示_第4页
struts实现分页显示_第5页
已阅读5页,还剩2页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

HYPERLINK”http://blog。chinauni/uid-332473—id-2132130.html”Elicpse+Struts实现数据分页显示2006—07—1111:23:16分类:Java一开发环境Elicpse+StrutsStudio+SqlServer2000+Tomcat。二开发思路既然讲的是Struts,那自然离不了MVC,分页显示也是如此.1建立适当的模型组件,对应你要查询数据库中的表。这部分由我们熟识的javaBean来充当。并在其中建立数据库查询方法,该方法需要一个java.sql。Conntection类型的参数,并返回一个ArrayList。在本例中为Book.java2建立分页所需要的模型组件,也是由javaBean来充当,通过由Book中供应的ArrayList来构造。本例中为PageBean。java.。3建立掌握器组件,这部分由Struts中的Action来实现.主要负责将实例化Book,并利用返回的ArrayList对象,构造PageBean。以及接收由视图传递而来的action参数.从而在PageBean对象中调用不同的方法,该方法返回Book[]对象.最后将Book[]和PageBean放入request中。本例中为PageListAction.java.4建立视图组件,这部分由jsp来充当,为了不消灭java代码,我们使用Struts供应的标签库,主要负责从request中取出刚刚放入的对象,通过反复调用PageListAction以及action参数,而实现分页显示。本例中为pagetest.jsp。5建立并配置struts-config。xml。6建立数据库。三实例代码ﻫﻫ1Book.javapackagebean;ﻫimportjava。sql。*;ﻫimportjava。util。ArrayList;ﻫ/**ﻫ*@作者吴步强ﻫ*Struts分页显示数据Bean,对应数据库中Book表ﻫ*/ﻫpublicclassBook{

privateStringbookname;//书名ﻫprivateStringauthor;//作者ﻫprivateStringprice;//价格ﻫﻫpublicBook(Stringname,Stringauthor,Stringprice){ﻫthis.bookname=name;ﻫthis。author=author;ﻫthis.price=price;ﻫ}ﻫ

publicStringgetAuthor(){

returnauthor;

}ﻫﻫpublicvoidsetAuthor(Stringauthor){ﻫthis.author=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数据查询已完成!");

while(resultSet.next())

{ﻫStringname=resultSet。getString("name”);

Stringauthor=resultSet.getString(”author");ﻫStringprice=resultSet.getString("price”);ﻫSystem。out。println("开头数据封装:name="+name+"author=”+author+"price="+price);ﻫBookbook=newBook(name,author,price);ﻫarrayList.add(book);

}ﻫconnection.close();ﻫresultSet。close();ﻫ}catch(SQLExceptione)ﻫ{ﻫSystem。out。println(”数据库特别”+e。toString());ﻫ}returnarrayList;ﻫ}

}2PageBean.javaﻫ

packagepage;ﻫimportbean.Book;

importjava。util.*;

/**

*@作者吴步强ﻫ*Struts分页显示规律Bean

*/ﻫpublicclassPageBean{

ﻫintcurrentPage=1;//当前页ﻫpublicinttotalPages=0;//总页数ﻫintpageRecorders=5;//每页5条数据ﻫinttotalRows=0;//总数据数ﻫintpageStartRow=0;//每页的起始数ﻫintpageEndRow=0;//每页显示数据的终止数ﻫ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()正在执行;");ﻫSystem.out。println(”参数currentPage="+currentPage);ﻫﻫif((currentPage-1)>0)

{

hasPreviousPage=true;ﻫ}ﻫelseﻫ{

hasPreviousPage=false;

ﻫif(currentPage〉=totalPages)ﻫ{

hasNextPage=false;ﻫ}ﻫelseﻫ{ﻫhasNextPage=true;ﻫ}ﻫSystem。out。println("参数hasNextPage="+hasNextPage);ﻫSystem.out.println("筹备执行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()开头执行;");ﻫﻫﻫ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("要显示的页面数据已经封装,简略信息如下:");ﻫthis.description();ﻫreturnbooks;

}ﻫ

publicStringtoString(inttemp)ﻫ{ﻫStringstr=Integer。toString(temp);ﻫreturnstr;

}ﻫ

publicvoiddescription()

{ﻫﻫStringdescription=”共有数据数:”+this。getTotalRows()+"共有页数:"+this.getTotalPages()+”当前页数为:"+this.getCurrentPage()+ﻫﻫ"是否有前一页:”+this.isHasPreviousPage()+"是否有下一页:”+this。isHasNextPage()+"开头行数:"+this.getPageStartRow()+”终止行数:"+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吴步强ﻫ*Struts分页显示Actionﻫ*/ﻫpublicclassPageListActionextendsAction{ﻫ

publicPageListAction(){}ﻫArrayListarrayList=newArrayList();ﻫPageBeanpb;ﻫﻫpublicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{ﻫStringaction;ﻫaction=request.getParameter(”action");

if(action==null||action.equals("null”)){//第一次读取数据ﻫtry{ﻫDataSourcedatasource=this.getDataSource(request,Constants.DATASOURCE_KEY);ﻫarrayList=Book.getAllBook(datasource.getConnection());ﻫSystem。out.println("第一步,数据已经成功传递到Action,action="+action);ﻫ}catch(Exceptione){ﻫe.printStackTrace();ﻫSystem.out.println("数据库连接消灭特别");

}ﻫﻫ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("参数action=”+action);ﻫSystem.out.println(”函数pb。getNextPage()筹备执行”);ﻫBook[]books=pb.getNextPage();

request.setAttribute("page",pb);ﻫrequest.setAttribute("result",books);ﻫ}ﻫif(action==”previousPage"||action.equals("previousPage”))ﻫ{ﻫSystem。out.println("参数action="+action);ﻫSystem.out.println("函数pb.getPreviouspage()筹备执行”);ﻫ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〉ﻫ共有数据总数<bean:writename=”page”property="totalRows"/〉;

共分<bean:writename=”page”property="totalPages"/>页,当前是第

〈bean:writename=”page"property=”currentPage”/>页ﻫ〈/body>ﻫ〈/html:html>ﻫ

5struts-config。xmlﻫ

<?xmlversion="1.0”encoding="ISO-8859-1"?>ﻫﻫ<!DOCTYPEstruts-configPUBLIC

"—//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN”ﻫ”http://jakarta。apache.org/struts/dtds/struts—config_1_1。dtd";><struts—config>

<data-sources>ﻫ<data—sourcekey="dataSource"type=”org.apache.commons.dbcp。BasicDataSource"〉

<set-propertyproperty=”driverClassName"value="com.microsoft.jdbc.sqlserver。SQLServerDriver"/〉ﻫ<set-propertyproperty="url"value=”jdbc

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论