struts分页.doc

上传人:PIYPING 文档编号:11375439 上传时间:2021-07-31 格式:DOC 页数:12 大小:49KB
返回 下载 相关 举报
struts分页.doc_第1页
第1页 / 共12页
struts分页.doc_第2页
第2页 / 共12页
struts分页.doc_第3页
第3页 / 共12页
struts分页.doc_第4页
第4页 / 共12页
struts分页.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《struts分页.doc》由会员分享,可在线阅读,更多相关《struts分页.doc(12页珍藏版)》请在三一文库上搜索。

1、在做之前我们的头脑中要有一个大概的框架:也就是我们怎么样来划分各个功能模块。一般来说一个分页功能框架至少要包含如下几个部分(java class)(括号注释对应本实例中的类)1:页面元素(Order.java)。也就是一个页面要显示的一条条的记录2:数据(DataCollection.java)。对应一个javaBean,我看有不少前辈将获取数据与页面控制放在一起,我本人认为这样做不好。一来逻辑不清楚,二来对于一个控制来说应是可重用的,但放在一起就不能重用了。3:页面页面控制(PageController.java)。也就是对一个页面的定义4:页面跳转的Action(PageAction.ja

2、va).因为这个页面跳转相对来说比较简单我们可以直接继承ActionForward来达到这个目的。5:用来显示的View.(page.jsp)*注:本实例是从一个客户定单中取数据*下面的我就各个部分作一个简单的介绍1:页面元素。也就是一个页面要显示的一条条的记录(Order.java)这个Order.java没有多大意思它只是一个“定单”的定义private int orderID;定单编号private String customerID;客户编号private Date orderDate;下单日期*/* Created on 2004-9-14* TODO To change the t

3、emplate for this generated file go to* Window - Preferences - Java - Code Style - Code Templates*/package com.toad.pub;import java.util.Date;/* author Administrator* TODO To change the template for this generated type comment go to* Window - Preferences - Java - Code Style - Code Templates*/public c

4、lass Order private int orderID;private String customerID;private Date orderDate;/* * */public Order() / TODO Auto-generated constructor stub/* * param orderID * param customerID * param orderDate */public Order(int orderID, String customerID, Date orderDate) this.orderID = orderID;this.customerID =

5、customerID;this.orderDate = orderDate;/* * return Returns the customerID. */public String getCustomerID() return customerID;/* * param customerID The customerID to set. */public void setCustomerID(String customerID) this.customerID = customerID;/* * return Returns the orderDate. */public Date getOrd

6、erDate() return orderDate;/* * param orderDate The orderDate to set. */public void setOrderDate(Date orderDate) this.orderDate = orderDate;/* * return Returns the orderID. */public int getOrderID() return orderID;/* * param orderID The orderID to set. */public void setOrderID(int orderID) this.order

7、ID = orderID;*2:数据(DataCollection.java).这个部分有您可以依据个人情况来进行自定义。我已有的环境中是用的连接池.private ArrayList list;用来存放所查询到的数据private Connection con;代表一个数据库连接。这个连接是通过传过来的。在接下来的view中我会作介绍*/* Created on 2004-9-15* TODO To change the template for this generated file go to* Window - Preferences - Java - Code Style - Cod

8、e Templates*/package com.toad.pub;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Date;/* author Administrator* TODO To change the template for this generated type comment go to* Window -

9、Preferences - Java - Code Style - Code Templates*/public class DataCollection private ArrayList list;private Connection con;/* * */public DataCollection() list=new ArrayList();/* * return Returns the con. */public Connection getCon() return con;/* * param con The con to set. */public void setCon(Con

10、nection con) this.con = con;/* * return Returns the list. */public ArrayList getList() return list;/* * param list The list to set. */public void setList() if(this.con=null)System.out.println(con is null!);return;String syntax=select orderid,customerid,orderdate from orders order by orderid asc;int

11、orderID=0;String customerID=null;Date orderDate=null;ResultSet rs=null;PreparedStatement pst=null;trypst=con.prepareStatement(syntax);rs=pst.executeQuery();while(rs.next()orderID=rs.getInt(1);customerID=rs.getString(2);orderDate=rs.getDate(3);list.add(new Order(orderID,customerID,orderDate);catch(SQ

12、LException e)System.out.println(SQLException occur at fetch datas !);finallytryrs.close();con.close();catch(SQLException e)System.out.println(SQLException occur at rs and con close() !);*3:页面页面控制(PageController.java)private ArrayList allItems;保存DataCollection中的list,也就是所有的数据private int pageNumber;当前页

13、的页号private int lastIndexOfPage;当前页的最后一个item(Order)在allItems中的indexprivate int itemsPerPage;每一页的容量private int itemsInPage;当前页的实际items(orders)private int lastPageNumber;为allItems/iemsPerPage or allItems/iemsPerPage+1private boolean hasPrevious;是否为第一页private boolean hasNext;是来为最后一页*/* Created on 2004-9

14、-14* TODO To change the template for this generated file go to* Window - Preferences - Java - Code Style - Code Templates*/package com.toad.pub;import java.util.ArrayList;/* author Administrator* TODO To change the template for this generated type comment go to* Window - Preferences - Java - Code St

15、yle - Code Templates*/public class PageController private int pageNumber;private int lastIndexOfPage;private int itemsPerPage;private int itemsInPage;private int lastPageNumber;private boolean hasPrevious;private boolean hasNext;private ArrayList allItems;/* * return Returns the allItems. */public A

16、rrayList getAllItems() return allItems;/* * param allItems The allItems to set. */public void setAllItems(ArrayList allItems) this.allItems = allItems;/* * return Returns the hasNext. */public boolean isHasNext() return hasNext;/* * param hasNext The hasNext to set. */public void setHasNext() int it

17、ems=pageNumber*itemsPerPage;if(items=allItems.size()this.hasNext =false;elsethis.hasNext=true;/* * return Returns the hasPrevious. */public boolean isHasPrevious() return hasPrevious;/* * param hasPrevious The hasPrevious to set. */public void setHasPrevious() if(pageNumber=1)this.hasPrevious=false;

18、elsethis.hasPrevious=true;/* * return Returns the itemsInPage. */public int getItemsInPage() return this.itemsInPage;/* * param itemsInPage The itemsInPage to set. */public void setItemsInPage() int temp=pageNumber*itemsPerPage;if(temp=allItems.size()this.itemsInPage=itemsPerPage;elsethis.itemsInPag

19、e=( allItems.size() - (pageNumber-1)*itemsPerPage );/* * return Returns the itemsPerPage. */public int getItemsPerPage() return itemsPerPage;/* * param itemsPerPage The itemsPerPage to set. */public void setItemsPerPage(int itemsPerPage) this.itemsPerPage = itemsPerPage;/* * return Returns the pageN

20、umber. */public int getPageNumber() return pageNumber;/* * param pageNumber The pageNumber to set. */public void setPageNumber(int pageNumber) this.pageNumber = pageNumber;/* * return Returns the lastIndexOfPage. */public int getLastIndexOfPage() return lastIndexOfPage;/* * param lastIndexOfPage The

21、 lastIndexOfPage to set. */public void setLastIndexOfPage() this.lastIndexOfPage =(pageNumber -1)*itemsPerPage;/* * return Returns the lastPageNumber. */public int getLastPageNumber() return lastPageNumber;/* * param lastPageNumber The lastPageNumber to set. */public void setLastPageNumber() if(allI

22、tems.size()%itemsPerPage=0)this.lastPageNumber =allItems.size()/itemsPerPage;elsethis.lastPageNumber =allItems.size()/itemsPerPage+1;*4:页面跳转的Action(PageAction.java).请注意PageAction是直接继承于ActionForward很简单:就是根据action=arg2.getParameter(action).trim();的返回值来设定pageNubmer和与之相就的设定。一定不能忘了调用 return super.execute

23、(arg0, arg1, arg2, arg3);*/* Created on 2004-9-14* TODO To change the template for this generated file go to* Window - Preferences - Java - Code Style - Code Templates*/package com.toad.pub;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servle

24、t.http.HttpSession;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.ForwardAction;import com.toad.pub.PageController;/* author Administrator* TODO To change the template for this ge

25、nerated type comment go to* Window - Preferences - Java - Code Style - Code Templates*/public class PageAction extends ForwardAction /* (non-Javadoc) * see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServ

26、letRequest, javax.servlet.http.HttpServletResponse) */public ActionForward execute(ActionMapping arg0, ActionForm arg1,HttpServletRequest arg2, HttpServletResponse arg3) throws Exception / TODO Auto-generated method stubString action=null;HttpSession session=arg2.getSession(true);action=arg2.getPara

27、meter(action).trim();PageController controller=(PageController)session.getAttribute(controller);int pageNumber=controller.getPageNumber();if(pareToIgnoreCase(next)=0)+pageNumber;else if(pareToIgnoreCase(pervious)=0)-pageNumber;else if(pareToIgnoreCase(first)=0)pageNumber=1;else if(pareToIgnoreCase(l

28、ast)=0)pageNumber=controller.getLastPageNumber();controller.setPageNumber(pageNumber);controller.setHasNext();controller.setHasPrevious();controller.setItemsInPage();controller.setLastIndexOfPage();return super.execute(arg0, arg1, arg2, arg3);*5:用来显示的View.(page.jsp)*/得到连接池对象/定义一个javaBean同时设定数据()jsp:setProperty name=Data property=con value=/定义一个PageController同时进行初始化请注意调用顺序!jsp:setProperty name=controller property=allItems value=/Lomboz JSP/hea

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1