<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    空間站

    北極心空

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

    解決Struts分頁(yè)顯示

    學(xué)習(xí)Struts已經(jīng)有2個(gè)多月了,前幾天群里的朋友問(wèn)我Struts分頁(yè)顯示的問(wèn)題,覺(jué)得好像與在jsp中的差不多,但還是遇到了這樣那樣的問(wèn)題,好不容易花了幾天時(shí)間把問(wèn)題都搞清楚,覺(jué)得還是寫點(diǎn)東西跟大家分享一下的好!
    至于Struts的語(yǔ)法這里就不多介紹了,不懂的朋友可以先看網(wǎng)上的其他文章。

    一 開(kāi)發(fā)環(huán)境
    Elicpse+Struts Studio+SqlServer2000+Tomcat。

    二 開(kāi)發(fā)思路
    既然講的是Struts,那自然離不了MVC,分頁(yè)顯示也是如此。

    1 建立適當(dāng)?shù)哪P徒M件,對(duì)應(yīng)你要查詢數(shù)據(jù)庫(kù)中的表。這部分由我們熟悉的javaBean來(lái)充當(dāng)。并在其中建立數(shù)據(jù)庫(kù)查詢方法,該方法需要一個(gè)java.sql.Conntection類型的參數(shù),并返回一個(gè)ArrayList。在本例中為 Book.java

    2 建立分頁(yè)所需要的模型組件,也是由javaBean來(lái)充當(dāng),通過(guò)由Book中提供的ArrayList來(lái)構(gòu)造。本例中為 PageBean.java.。

    3建立控制器組件,這部分由Struts 中的Action來(lái)實(shí)現(xiàn)。主要負(fù)責(zé)將實(shí)例化Book,并利用返回的ArrayList對(duì)象,構(gòu)造PageBean。以及接收由視圖傳遞而來(lái)的action參數(shù)。從而在PageBean對(duì)象中調(diào)用不同的方法,該方法返回Book[] 對(duì)象。最后將 Book[]和PageBean放入request中。本例中為PageListAction.java。

    4建立視圖組件,這部分由jsp來(lái)充當(dāng),為了不出現(xiàn)java 代碼,我們使用Struts提供的標(biāo)簽庫(kù),主要負(fù)責(zé)從request中取出剛剛放入的對(duì)象,通過(guò)反復(fù)調(diào)用PageListAction以及action參數(shù),而實(shí)現(xiàn)分頁(yè)顯示。本例中為pagetest.jsp.
    5 建立并配置struts-config.xml。
    6 建立數(shù)據(jù)庫(kù)。

    三 實(shí)例代碼
    1 Book.java

    package bean; 
    import java.sql.*
    import java.util.ArrayList; 
    /** 
     * @作者 李敏強(qiáng) 
     * Struts分頁(yè)顯示數(shù)據(jù)Bean,對(duì)應(yīng)數(shù)據(jù)庫(kù)中Book表 
     
    */ 
    public class Book { 
     
    private String bookname; //書名 
     private String author;   //作者 
     private String price;    //價(jià)格 
      
    public Book(String name,String author,String price){ 
     
    this.bookname=name; 
     
    this.author=author; 
     
    this.price=price; 


     
    public String getAuthor() { 
      
    return author; 
     } 

     
    public void setAuthor(String author) { 
      
    this.author = author; 
     } 

     
    public String getBookname() { 
      
    return bookname; 
     } 

     
    public void setBookname(String bookname) { 
      
    this.bookname = bookname; 
     } 
      
     
    public String getPrice(){ 
         
    return this.price;  
     } 
      
     
    public void setPrice(String price){ 
         
    this.price=price;  
     } 
      
     
    public static ArrayList getAllBook(Connection connection){ 
       String sql
    ="select * from book"
       ArrayList arrayList 
    = new ArrayList(); 
       
    try
       Statement statement 
    = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
       ResultSet resultSet 
    = statement.executeQuery(sql);   
             System.out.println(
    "BookBean 數(shù)據(jù)查詢已完成!"); 
          
    while(resultSet.next()) 
          {   
            String name 
    = resultSet.getString("name"); 
            String author 
    = resultSet.getString("author"); 
            String price 
    = resultSet.getString("price"); 
            System.out.println(
    "開(kāi)始數(shù)據(jù)封裝:name="+name+"author="+author+"price="+price); 
            Book book 
    = new Book(name,author,price);        
            arrayList.add(book); 
          } 
        connection.close(); 
        resultSet.close(); 
       }
    catch(SQLException e) 
       { 
        System.out.println(
    "數(shù)據(jù)庫(kù)異常"+e.toString()); 
       } 

          
    return arrayList; 
     } 

    2 PageBean.java

    package page; 
    import bean.Book; 
    import java.util.*
    /** 
     * @作者 李敏強(qiáng) 
     * Struts分頁(yè)顯示邏輯Bean 
     
    */ 
    public class PageBean { 

     
    int currentPage=1;  //當(dāng)前頁(yè) 
    public int totalPages=0;  //總頁(yè)數(shù) 
     int pageRecorders=5;//每頁(yè)5條數(shù)據(jù) 
     int totalRows=0;  //總數(shù)據(jù)數(shù) 
     int pageStartRow=0;//每頁(yè)的起始數(shù) 
     int pageEndRow=0;  //每頁(yè)顯示數(shù)據(jù)的終止數(shù) 
     boolean hasNextPage=false//是否有下一頁(yè) 
     boolean hasPreviousPage=false//是否有前一頁(yè) 
     ArrayList arrayList; 
     Iterator it; 
    public PageBean(){} 
      
    public PageBean(ArrayList arrayList){ 
     
    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;    
        } 



     
    /** 
      * 
    @return Returns the currentPage. 
      
    */ 
     
    public String getCurrentPage() { 
      
    return this.toString(currentPage); 
     } 
     
    /** 
      * 
    @param currentPage The currentPage to set. 
      
    */ 
     
    public void setCurrentPage(int currentPage) { 
      
    this.currentPage = currentPage; 
     } 
     
    /** 
      * 
    @return Returns the pageRecorders. 
      
    */ 
     
    public int getPageRecorders() { 
      
    return pageRecorders; 
     } 
     
    /** 
      * 
    @param pageRecorders The pageRecorders to set. 
      
    */ 
     
    public void setPageRecorders(int pageRecorders) { 
      
    this.pageRecorders = 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 this.toString(totalPages); 
     } 
     
    /** 
      * 
    @return Returns the totalRows. 
      
    */ 
     
    public String getTotalRows() { 
      
    return this.toString(totalRows); 
     } 
     
    /** 
      * 
    @return Returns the hasNextPage. 
      
    */ 
     
    public boolean isHasNextPage() { 
      
    return hasNextPage; 
     } 
     
    /** 
      * 
    @param hasNextPage The hasNextPage to set. 
      
    */ 
     
    public void setHasNextPage(boolean hasNextPage) { 
      
    this.hasNextPage = hasNextPage; 
     } 
     
    /** 
      * 
    @return Returns the hasPreviousPage. 
      
    */ 
     
    public boolean isHasPreviousPage() { 
      
    return hasPreviousPage; 
     } 
     
    /** 
      * 
    @param hasPreviousPage The hasPreviousPage to set. 
      
    */ 
     
    public void setHasPreviousPage(boolean hasPreviousPage) { 
      
    this.hasPreviousPage = hasPreviousPage; 
     } 
    public Book[] 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ǔn)備執(zhí)行PageBean.getBooks()"); 
     Book[] books
    =getBooks(); 
     
    this.description(); 
      
     
    return books; 


    public Book[] 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(); 
     
    return books; 


    public Book[] getBooks(){ 
     System.out.println(
    "pageBean.getBooks()開(kāi)始執(zhí)行;"); 
      
      
     
    if(currentPage*pageRecorders<totalRows){//判斷是否為最后一頁(yè) 
      pageEndRow=currentPage*pageRecorders; 
         pageStartRow
    =pageEndRow-pageRecorders; 
     } 
     
    else
      pageEndRow
    =totalRows; 
      pageStartRow
    =pageRecorders*(totalPages-1); 
     } 
     Book[] books
    =new Book[pageEndRow-pageStartRow+1]; 
      
     System.out.println(
    "pageStartRow="+pageStartRow); 
     System.out.println(
    "pageEndRow="+pageEndRow); 
      
    int j=0;  
     
    for(int i=pageStartRow;i<pageEndRow;i++
     { 
      
      Book book
    =(Book)arrayList.get(i);  
      books[j
    ++]=book; 
      
     } 
     System.out.println(
    "要顯示的頁(yè)面數(shù)據(jù)已經(jīng)封裝,具體信息如下:"); 
     
    this.description(); 
     
    return books; 


    public String toString(int temp) 

    String str
    =Integer.toString(temp); 
    return str; 


    public void description() 


       String description
    ="共有數(shù)據(jù)數(shù):"+this.getTotalRows()+ 

       
    "共有頁(yè)數(shù): "+this.getTotalPages() + 

       
    "當(dāng)前頁(yè)數(shù)為:"+this.getCurrentPage()+ 
        
       
    " 是否有前一頁(yè): "+this.isHasPreviousPage() + 

       
    " 是否有下一頁(yè):"+this.isHasNextPage()+ 

       
    " 開(kāi)始行數(shù):"+this.getPageStartRow()+ 

       
    " 終止行數(shù):"+this.getPageEndRow(); 

       System.out.println(description); 



    3  PageListAction.java
    package page; 
    import org.apache.struts.action.*
    import javax.servlet.http.*
    import comm.Constants; 

    import bean.Book; 
    import java.util.*
    import javax.sql.DataSource; 
    /** 
     * 
    @author 李敏強(qiáng) 
     * Struts分頁(yè)顯示Action 
     
    */ 
    public class PageListAction extends Action { 

     
    public PageListAction(){} 
     ArrayList arrayList
    =new ArrayList(); 
        PageBean pb; 
      
     
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {    
    String action;  
    action
    =request.getParameter("action");    
    if(action==null || action.equals("null")){ //第一次讀取數(shù)據(jù) 
    try
    DataSource datasource
    =this.getDataSource(request,Constants.DATASOURCE_KEY);     
    arrayList
    =Book.getAllBook(datasource.getConnection()); 
    System.out.println(
    "第一步,數(shù)據(jù)已經(jīng)成功傳遞到Action,action="+action); 
       }
    catch(Exception e){ 
              e.printStackTrace(); 
      System.out.println(
    "數(shù)據(jù)庫(kù)連接出現(xiàn)異常"); 
          }  
        
         pb
    =new PageBean(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ǔn)備執(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ǔn)備執(zhí)行"); 
      Book[] books
    =pb.getPreviouspage();   
      request.setAttribute(
    "page",pb); 
                   request.setAttribute(
    "result",books); 
         
        } 
       } 
       
    return (mapping.findForward("success")); 
      }  

    4 pagetest.jsp
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> 
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 
    <%@ page contentType="text/html; charset=gb2312" language="java"%> 

    <html:html locale="true"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    </head> 
    <body> 

    <table border="1"> 
    <tr><th>書名</th><th>作者</th><th>價(jià)格</th></tr> 
    <logic:present name="result"> 
    <logic:iterate id="book" name="result"  type="bean.Book" > 
    <logic:present name="book"> 
    <tr> 
     
    <td><bean:write name="book" property="bookname" /></td> 
     
    <td> <bean:write name="book" property="author" /></td> 
     
    <td><bean:write name="book" property="price" /></td> 
    </tr> 
    </logic:present> 
    </logic:iterate> 
    </logic:present> 
    </table> 
    <logic:equal name="page" property="hasNextPage" value="true"> 
    <html:link page="/page.do?action=nextPage">nextPage</html:link>  
    </logic:equal> 
    <logic:equal name="page" property="hasPreviousPage" value="true"> 
    <html:link page="/page.do?action=previousPage">PreviousPage</html:link> 
    </logic:equal> 
    共有數(shù)據(jù)總數(shù)
    <bean:write name="page" property="totalRows"/>
    共分
    <bean:write name="page" property="totalPages"/>頁(yè),當(dāng)前是第 
    <bean:write name="page" property="currentPage"/>頁(yè) 
    </body> 
    </html:html> 

    5 struts-config.xml
    <?xml version="1.0" encoding="ISO-8859-1" ?> 

    <!DOCTYPE struts-config PUBLIC 
              "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" 
              "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd";
    > 

    <struts-config> 
     
    <data-sources> 
      
    <data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource"> 
       
    <set-property property="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> 
       
    <set-property property="url" value="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=eBookStore;SelectMethod=cursor"/> 
       
    <set-property property="username" value="limq"/> 
       
    <set-property property="password" value="1"/> 
       
    <set-property property="maxActive" value="10"/> 
       
    <set-property property="maxWait" value="5000"/> 
       
    <set-property property="defaultAutoCommit" value="true"/> 
       
    <set-property property="defaultReadOnly" value="false"/> 
      
    </data-source> 
     
    </data-sources> 
      
    <form-beans> 
      
    </form-beans> 
      
    <global-forwards> 
      
    </global-forwards> 
      
    <action-mappings> 
      
    <action path="/page" type="page.PageListAction" scope="request"> 
      
    <forward name="success" path="/pagetest.jsp"/> 
      
    </action> 
      
    </action-mappings> 
      
    <controller> 
      
    </controller> 
    </struts-config> 
    6 建立eBookStore數(shù)據(jù)庫(kù),以及表book(name,author,parce);其中數(shù)據(jù)的配置可以根據(jù)你的不同情況在struts-config.xml中而定。

    7 Constants.java 
    package comm; 

    /** 
     * this interface provides the constant string for applicator constant 
     
    */ 
    public class Constants { 
      
    /** 
        * name of the User Object in HttpSession 
        
    */ 
       
    public static String USER_KEY="user"
       
    /** 
        * dataSource name 
        
    */ 
       
    public static String DATASOURCE_KEY="dataSource"

    posted on 2006-11-02 14:26 蘆葦 閱讀(386) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 免费观看四虎精品成人| 免费国产黄网站在线观看视频 | 最近更新免费中文字幕大全| 99久久久精品免费观看国产| 国产亚洲成归v人片在线观看| 香蕉大伊亚洲人在线观看| 国产精品视频白浆免费视频| 日韩免费观看视频| 亚洲成人免费电影| 免费成人高清在线视频| 国产一级高清免费观看| 亚洲国产精品成人精品小说| 少妇性饥渴无码A区免费| 免费观看日本污污ww网站一区| 亚洲国产成人久久77| 男人都懂www深夜免费网站| 免费一级一片一毛片| 久久亚洲精品国产亚洲老地址| 污视频在线观看免费| 国产精品亚洲玖玖玖在线观看| 亚洲欧美日韩一区二区三区| **aaaaa毛片免费同男同女| 国产亚洲综合色就色| 无码日韩人妻AV一区免费l | 亚洲无圣光一区二区| 国产一区二区三区免费观看在线| 免费大黄网站在线观| 亚洲精品无码国产片| 黄页网站免费观看| 亚洲视频免费播放| 国产情侣久久久久aⅴ免费| 亚洲一级特黄大片在线观看 | 深夜福利在线视频免费| 日本人的色道www免费一区| 国产亚洲国产bv网站在线 | 免费下载成人电影| 亚洲手机中文字幕| 麻豆视频免费播放| 亚洲精品欧洲精品| 中文字幕亚洲免费无线观看日本| 亚洲av不卡一区二区三区|