涓嬮潰鐨勬垜灝卞悇涓儴鍒嗕綔涓涓畝鍗曠殑浠嬬粛
1:欏甸潰鍏冪礌銆備篃灝辨槸涓涓〉闈㈣鏄劇ず鐨勪竴鏉℃潯鐨勮褰?Order.java)
榪欎釜Order.java娌℃湁澶氬ぇ鎰忔濆畠鍙槸涓涓?#8220;瀹氬崟”鐨勫畾涔?br />
private int orderID;銆銆銆 瀹氬崟緙栧彿
private String customerID; 瀹㈡埛緙栧彿
private Date orderDate; 涓嬪崟鏃ユ湡
***********************************************************
/*
* 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 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 class 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 = 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 getOrderDate() {
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.orderID = orderID;
}
***********************************************************
2:鏁版嵁(DataCollection.java).榪欎釜閮ㄥ垎鏈夋偍鍙互渚濇嵁涓漢鎯呭喌鏉ヨ繘琛?br />
鑷畾涔夈傛垜宸叉湁鐨勭幆澧冧腑鏄敤鐨勮繛鎺ユ睜.
private ArrayList list; 鐢ㄦ潵瀛樻斁鎵鏌ヨ鍒扮殑鏁版嵁
private Connection con;銆浠h〃涓涓暟鎹簱榪炴帴銆傝繖涓繛鎺?br />
鏄氳繃<jsp:setProperty../>浼犺繃鏉ョ殑銆傚湪鎺ヤ笅鏉ョ殑view涓垜浼氫綔浠嬬粛
***********************************************************
/*
* Created on 2004-9-15
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code 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 - 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(Connection 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 orderID=0;
String customerID=null;
Date orderDate=null;
ResultSet rs=null;
PreparedStatement pst=null;
try{
pst=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(SQLException e){
System.out.println("SQLException occur at fetch datas !");
}finally{
try{
rs.close();
con.close();
}catch(SQLException e){
System.out.println("SQLException occur at rs and con close() !");
}
}
}
}
***********************************************************
3:欏甸潰欏甸潰鎺у埗錛圥ageController.java錛?br />
private ArrayList allItems; 淇濆瓨DataCollection涓殑list錛屼篃灝辨槸鎵鏈夌殑鏁版嵁
private int pageNumber; 褰撳墠欏電殑欏靛彿
private int lastIndexOfPage; 褰撳墠欏電殑鏈鍚庝竴涓猧tem(Order)鍦╝llItems涓殑index
private int itemsPerPage; 姣忎竴欏電殑瀹歸噺
private int itemsInPage; 褰撳墠欏電殑瀹為檯items(orders)
private int lastPageNumber; 涓篴llItems/iemsPerPage or allItems/iemsPerPage+1
private boolean hasPrevious; 鏄惁涓虹涓欏?br />
private boolean hasNext; 鏄潵涓烘渶鍚庝竴欏?br />
***********************************************************
/*
* 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 java.util.ArrayList;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - 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 ArrayList 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 items=pageNumber*itemsPerPage;
if(items>=allItems.size()){
this.hasNext =false;
}else{
this.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;
}else{
this.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;
}else{
this.itemsInPage=( 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 pageNumber.
*/
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 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(allItems.size()%itemsPerPage==0){
this.lastPageNumber =allItems.size()/itemsPerPage;
}else{
this.lastPageNumber =allItems.size()/itemsPerPage+1;
}
}
}
***********************************************************
4:欏甸潰璺寵漿鐨凙ction(PageAction.java).
璇鋒敞鎰廝ageAction鏄洿鎺ョ戶鎵夸簬ActionForward
寰堢畝鍗曪細灝辨槸鏍規嵁action=arg2.getParameter("action").trim();
鐨勮繑鍥炲兼潵璁懼畾pageNubmer鍜屼笌涔嬬浉灝辯殑璁懼畾銆?br />
涓瀹氫笉鑳藉繕浜嗚皟鐢?return super.execute(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;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PageAction extends ForwardAction {