熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

漫談解決Struts分頁顯示

2022-06-13   來源: Java開源技術 

  學習Struts已經有個多月了前幾天群裡的朋友問我Struts分頁顯示的問題覺得好像與在jsp中的差不多但還是遇到了這樣那樣的問題好不容易花了幾天時間把問題都搞清楚覺得還是寫點東西跟大家分享一下的好!
  至於Struts的語法這裡就不多介紹了不懂的朋友可以先看網上的其他文章
  
  一 開發環境
  Elicpse+Struts Studio+SqlServer+Tomcat
  
  二 開發思路
  既然講的是Struts那自然離不了MVC分頁顯示也是如此
  
   建立適當的模型組件對應你要查詢數據庫中的表這部分由我們熟悉的javaBean來充當並在其中建立數據庫查詢方法該方法需要一個javasqlConntection類型的參數並返回一個ArrayList在本例中為 Bookjava
  
   建立分頁所需要的模型組件也是由javaBean來充當通過由Book中提供的ArrayList來構造本例中為 PageBeanjava
  
  建立控制器組件這部分由Struts 中的Action來實現主要負責將實例化Book並利用返回的ArrayList對象構造PageBean以及接收由視圖傳遞而來的action參數從而在PageBean對象中調用不同的方法該方法返回Book[] 對象最後將 Book[]和PageBean放入request中本例中為PageListActionjava
  
  建立視圖組件這部分由jsp來充當為了不出現java 代碼我們使用Struts提供的標簽庫主要負責從request中取出剛剛放入的對象通過反復調用PageListAction以及action參數而實現分頁顯示本例中為pagetestjsp
   建立並配置strutsconfigxml
   建立數據庫
  
  三 實例代碼
   Bookjava
  
  package bean;
  import javasql*;
  import javautilArrayList;
  /**
  * Struts分頁顯示數據Bean對應數據庫中Book表
  */
  public class Book {
  private String bookname; //書名
  private String author;  //作者
  private String price;  //價格
  
  public Book(String nameString authorString price){
  thisbookname=name;
  thisauthor=author;
  thisprice=price;
  }
  
  public String getAuthor() {
  return author;
  }
  
  public void setAuthor(String author) {
  thisauthor = author;
  }
  
  public String getBookname() {
  return bookname;
  }
  
  public void setBookname(String bookname) {
  thisbookname = bookname;
  }
  
  public String getPrice(){
  return thisprice;
  }
  
  public void setPrice(String price){
  thisprice=price;
  }
  
  public static ArrayList getAllBook(Connection connection){
  String sql=select * from book;
  ArrayList arrayList = new ArrayList();
  try{
  Statement statement = connectioncreateStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY);
  ResultSet resultSet = statementexecuteQuery(sql);
  Systemoutprintln(BookBean 數據查詢已完成!);
  while(resultSetnext())
  {
  String name = resultSetgetString(name);
  String author = resultSetgetString(author);
  String price = resultSetgetString(price);
  Systemoutprintln(開始數據封裝name=+name+author=+author+price=+price);
  Book book = new Book(nameauthorprice);
  arrayListadd(book);
  }
  connectionclose();
  resultSetclose();
  }catch(SQLException e)
  {
  Systemoutprintln(數據庫異常+etoString());
  }
  
  return arrayList;
  }
  }
  
   PageBeanjava
  package page;
  import beanBook;
  import javautil*;
  /**
  * Struts分頁顯示邏輯Bean
  */
  public class PageBean {
  
  int currentPage=; //當前頁
  public int totalPages=; //總頁數
  int pageRecorders=;//每頁條數據
  int totalRows=; //總數據數
  int pageStartRow=;//每頁的起始數
  int pageEndRow=; //每頁顯示數據的終止數
  boolean hasNextPage=false; //是否有下一頁
  boolean hasPreviousPage=false; //是否有前一頁
  ArrayList arrayList;
  Iterator it;
  public PageBean(){}
  
  public PageBean(ArrayList arrayList){
  thisarrayList=arrayList;
  totalRows=arrayListsize();
  it=erator();
  hasPreviousPage=false;
  currentPage=;
  if((totalRows%pageRecorders)==)
  {
  totalPages=totalRows/pageRecorders;
  }
  else
  {
  totalPages=totalRows/pageRecorders+;
  }
  
  if(currentPage>=totalPages)
  {
  hasNextPage=false;
  }
  else
  {
  hasNextPage=true;
  }
  
  
  if(totalRows<pageRecorders)
  {
  thispageStartRow=;
  thispageEndRow=totalRows;
  }
  else
  {
  thispageStartRow=;
  thispageEndRow=pageRecorders;
  }
  
  }
  
  /**
  * @return Returns the currentPage
  */
  public String getCurrentPage() {
  return thistoString(currentPage);
  }
  /**
  * @param currentPage The currentPage to set
  */
  public void setCurrentPage(int currentPage) {
  thiscurrentPage = currentPage;
  }
  /**
  * @return Returns the pageRecorders
  */
  public int getPageRecorders() {
  return pageRecorders;
  }
  /**
  * @param pageRecorders The pageRecorders to set
  */
  public void setPageRecorders(int pageRecorders) {
  thispageRecorders = pageRecorders;
  }
  /**
  * @return Returns the pageEndRow
  */
  public int getPageEndRow() {
  return pageEndRow;
  }
  /**
  * @return Returns the pageStartRow
  */
  public int getPageStartRow() {
  return pageStartRow;
  }
  /**
  * @return Returns the totalPages
  */
  public String getTotalPages() {
  
  return thistoString(totalPages);
  }
  /**
  * @return Returns the totalRows
  */
  public String getTotalRows() {
  return thistoString(totalRows);
  }
  /**
  * @return Returns the hasNextPage
  */
  public boolean isHasNextPage() {
  return hasNextPage;
  }
  /**
  * @param hasNextPage The hasNextPage to set
  */
  public void setHasNextPage(boolean hasNextPage) {
  thishasNextPage = hasNextPage;
  }
  /**
  * @return Returns the hasPreviousPage
  */
  public boolean isHasPreviousPage() {
  return hasPreviousPage;
  }
  /**
  * @param hasPreviousPage The hasPreviousPage to set
  */
  public void setHasPreviousPage(boolean hasPreviousPage) {
  thishasPreviousPage = hasPreviousPage;
  }
  public Book[] getNextPage(){
  
  currentPage=currentPage+;
  Systemoutprintln(PageBeangetNextPage()正在執行);
  Systemoutprintln(參數currentPage=+currentPage);
  
  if((currentPage)>)
  {
  hasPreviousPage=true;
  }
  else
  {
  hasPreviousPage=false;
  }
  
  if(currentPage>=totalPages)
  {
  hasNextPage=false;
  }
  else
  {
  hasNextPage=true;
  }
  Systemoutprintln(參數hasNextPage=+hasNextPage);
  Systemoutprintln(准備執行PageBeangetBooks());
  Book[] books=getBooks();
  thisdescription();
  
  return books;
  }
  
  public Book[] getPreviouspage(){
  
  currentPage=currentPage;
  
  if(currentPage==){currentPage=;}
  
  if(currentPage>=totalPages)
  {
  hasNextPage=false;
  }
  else
  {
  hasNextPage=true;
  }
  if((currentPage)>)
  {
  hasPreviousPage=true;
  }
  else
  {
  hasPreviousPage=false;
  }
  Book[] books=getBooks();
  thisdescription();
  return books;
  }
  
  public Book[] getBooks(){
  Systemoutprintln(pageBeangetBooks()開始執行);
  

From:http://tw.wingwit.com/Article/program/Java/ky/201311/28690.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.