乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java標(biāo)簽分頁實(shí)現(xiàn)

       CEO悟飯 2015-04-29

      Java實(shí)現(xiàn)標(biāo)簽分頁

       

            最近為了開發(fā)一個(gè)網(wǎng)站,里面要用分頁功能,但是之前很少自己寫分頁標(biāo)簽,又不想用現(xiàn)成框架。所以自己參考了些資料,寫了個(gè)分頁例子測(cè)試了一下。

            代碼主要分為三個(gè)類:

      1. PageTag 分頁標(biāo)簽類
      2. Page 分頁bean
      3. Constant 設(shè)置常量

           Page代碼:

      Java代碼  收藏代碼
      1. /** 
      2.  *  
      3.  * @author byyang 
      4.  *  
      5.  */  
      6. public class Page  
      7. {  
      8.     private int current = 0;  //當(dāng)前頁,默認(rèn)為第一頁  
      9.     private int size;     //記錄總數(shù)  
      10.     private int length;   //每頁的長(zhǎng)度  
      11.     private String url;   //訪問路徑  
      12.       
      13.     public Page(int offset, int size, int length) {  
      14.         super();  
      15.         this.current = offset;  
      16.         this.size = size;  
      17.         this.length = length;  
      18.     }  
      19.       
      20.     /** 
      21.      * 獲取總頁數(shù) 
      22.      * @return 
      23.      */  
      24.     public int pageCount(){  
      25.         int pagecount = 0;  
      26.         if(this.size % this.length == 0){  
      27.             pagecount = this.size / this.length;  
      28.         }else{  
      29.             pagecount = this.size / this.length + 1;  
      30.         }  
      31.           
      32.         return pagecount;  
      33.     }  
      34.       
      35.     //最后一頁開始條數(shù)  
      36.     public int lastPageOffset(){  
      37.         return this.size - lastPageSize();  
      38.     }  
      39.       
      40.     //最后一頁頁大小  
      41.     public int lastPageSize(){  
      42.         int lastpagesize = 0;  
      43.         if(this.size % this.length == 0){  
      44.             lastpagesize = this.length;  
      45.         }else{  
      46.             lastpagesize = this.size % this.length;  
      47.         }  
      48.         return lastpagesize;  
      49.     }  
      50.       
      51.     //獲取起始頁  
      52.     public int getOffset() {  
      53.         return current;  
      54.     }  
      55.     //總記錄數(shù)  
      56.     public int getSize() {  
      57.         return size;  
      58.     }  
      59.     //每頁大小  
      60.     public int getLength() {  
      61.         return length;  
      62.     }  
      63.     //獲取訪問路徑  
      64.     public String getUrl() {  
      65.         return url;  
      66.     }  
      67.       
      68.     //上一頁  
      69.     public int getLastOffset(){  
      70.         int offset = this.getOffset() - this.getLength();  
      71.         if(offset > 0){  
      72.             return offset;  
      73.         }else{  
      74.             return 0;  
      75.         }  
      76.           
      77.     }  
      78.     //下一頁  
      79.     public int getNextOffset(){  
      80.         int offset =  this.getOffset() + this.getLength();  
      81.         if(offset > this.getSize()){  
      82.             return getLastOffset();  
      83.         }else{  
      84.             return offset;  
      85.         }  
      86.     }  
      87.       
      88.     public String getPageNavigation(){  
      89.         String pageNavigation = "";  
      90.         return pageNavigation;  
      91.     }  
      92.   
      93.     public void setOffset(int offset) {  
      94.         this.current = offset;  
      95.     }  
      96.   
      97.     public void setSize(int size) {  
      98.         this.size = size;  
      99.     }  
      100.   
      101.     public void setLength(int length) {  
      102.         this.length = length;  
      103.     }  
      104.   
      105.     public void setUrl(String url) {  
      106.         this.url = url;  
      107.     }  
      108. }  

       

       

         現(xiàn)在我們要實(shí)現(xiàn)PageTag標(biāo)簽類,自定義標(biāo)簽有兩種實(shí)現(xiàn)TagSupport和SimpleTagSupport,兩個(gè)都能實(shí)現(xiàn)這個(gè)功能,這個(gè)看個(gè)人選擇了。

       

       

      Java代碼  收藏代碼
      1. import java.io.IOException;  
      2. import javax.servlet.http.HttpServletRequest;  
      3. import javax.servlet.jsp.JspException;  
      4. import javax.servlet.jsp.JspWriter;  
      5. import javax.servlet.jsp.tagext.TagSupport;  
      6.   
      7. import com.byyang.util.Constants;  
      8. /** 
      9.  *  doStartTag()和doEndTag()返回值處理: 
      10.  * SKIP_BODY (0) :跳過了開始和結(jié)束標(biāo)簽之間的代碼。 
      11.  * EVAL_BODY_INCLUDE(1):將body的內(nèi)容輸出到存在的輸出流中 
      12.  * SKIP_PAGE(5): 忽略剩下的頁面。 
      13.  * EVAL_PAGE隱含(6):繼續(xù)執(zhí)行下面的頁 
      14.  * @author yangby 
      15.  * 
      16.  */  
      17. public class PageTag extends TagSupport{  
      18.     
      19.    private String url;  
      20.    
      21.    public String getUrl(){  
      22.      return this.url;  
      23.    }  
      24.    
      25.    public void setUrl(String url) {  
      26.      this.url = url;  
      27.    }  
      28.    
      29.    public int doEndTag()throws JspException{  
      30.      return EVAL_PAGE;  
      31.    }  
      32.     
      33.   
      34.  public int doStartTag() throws JspException{  
      35.      HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();  
      36.      //獲取分頁bean  
      37.      Page page = (Page)request.getAttribute("pager");  
      38.        
      39.      JspWriter out = this.pageContext.getOut();  
      40.      //拼接分頁信息  
      41.      StringBuilder s = new StringBuilder();  
      42.      s.append("共 " + ((page.getSize() - 1) / page.getLength() + 1) + " 頁   ");  
      43.    
      44.      //設(shè)置當(dāng)前頁第一條記錄索引  
      45.      String current = request.getParameter("current");  
      46.      if(current != null && !"".equals(current)){  
      47.       page.setOffset(Integer.parseInt(current));  
      48.      }  
      49.      if (page.getSize() > page.getLength()){  
      50.     //獲取連接符pref  
      51.     String pref;  
      52.     if (this.url.indexOf("?") > -1){  
      53.     pref = "&";  
      54.     }else {  
      55.     pref = "?";  
      56.     }  
      57.     this.doStart(s, page, pref);  
      58.     this.doBody(s, page, pref);  
      59.     this.doEnd(s,page,pref);  
      60.      }else{  
      61.        s.append("    ");  
      62.      }  
      63.      s.append(" 共 " + page.getSize() + " 條記錄");  
      64.      try{  
      65.        out.println(s.toString());  
      66.      }catch (IOException e){  
      67.        e.printStackTrace();  
      68.      }  
      69.        
      70.      return SKIP_BODY;  
      71.    }  
      72.    
      73.  /** 
      74.   * //拼接"首頁"和"<" 
      75.   * @param s 
      76.   * @param page 
      77.   * @param pref 
      78.   */  
      79.  public void doStart(StringBuilder s,Page page,String pref){  
      80.        if (page.getOffset() > 0){  
      81.         s.append("<a href='" + this.url + pref +   
      82.            "current=0'>[首頁]</a>\n" + "<a href='" + this.url + pref +   
      83.            "current=" + page.getLastOffset() + "'>[<]</a>\n");  
      84.        }else{  
      85.         s.append("<a href='" + this.url + pref + "current=0'>[首頁]</a>\n" );  
      86.        }  
      87.  }  
      88.    
      89.  /** 
      90.   * 拼接"尾頁"和">" 
      91.   * @param s 
      92.   * @param page 
      93.   * @param pref 
      94.   */  
      95.  public void doEnd(StringBuilder s,Page page,String pref){  
      96.        if (page.getOffset() < page.getSize() - page.getLength()) {  
      97.         s.append("<a href='" + this.url + pref + "current=" + page.getNextOffset() + "'>[>]</a>\n" + "<a href='" +   
      98.            this.url + pref + "current=" + page.lastPageOffset() +   
      99.            "'>[尾頁]</a>\n");  
      100.        }else{  
      101.         s.append("\n"+ "<a href='" +  this.url + pref + "current=" + page.lastPageOffset() + "'>[尾頁]</a>\n");  
      102.        }  
      103.  }  
      104.    
      105.  /** 
      106.   * 數(shù)字連接 
      107.   * @param s 
      108.   * @param page 
      109.   * @param pref 
      110.   */  
      111.  public void doBody(StringBuilder s,Page page,String pref){  
      112.    //顯示半徑  
      113.        int radius = Constants.MAX_PAGE_INDEX / 2 * page.getLength();  
      114.         
      115.        //顯示數(shù)字的連接從第幾條開始  
      116.        int startOffset;  
      117.        if ((page.getOffset() < radius) || (page.pageCount() <= Constants.MAX_PAGE_INDEX)){  
      118.         startOffset = 0;  
      119.        }else{  
      120.          if (page.getOffset() < page.getSize() - radius){  
      121.            startOffset = page.getOffset() - radius;  
      122.          }else{  
      123.            startOffset = (page.getSize() / page.getLength() - Constants.MAX_PAGE_INDEX) *   
      124.              page.getLength();  
      125.          }  
      126.        }  
      127.        for (int i = startOffset; (i < page.getSize()) && (i < startOffset + Constants.MAX_PAGE_INDEX * page.getLength()); ){  
      128.          if (i == page.getOffset()){  
      129.            s.append("<b>" + (i / page.getLength() + 1) + "</b>\n");  
      130.          }else{  
      131.           s.append("<a href='" + this.url + pref + "current=" + i +   
      132.             "'>" + (i / page.getLength() + 1) + "</a>\n");  
      133.          }  
      134.          i += page.getLength();  
      135.        }  
      136.  }  
      137. }  

       靜態(tài)參數(shù)類:

      Java代碼  收藏代碼
      1. public class Constants {  
      2.       
      3.     public static int PAGE_SIZE = 5; //每頁大小  
      4.     public static int MAX_PAGE_INDEX = 5;//最多顯示5個(gè)數(shù)字連接  
      5.       
      6. }  

       

      編寫tld文件mypage.tld: 

       

      Xml代碼  收藏代碼
      1. <?xml version="1.0" encoding="utf-8" ?>  
      2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java./dtd/web-jsptaglibrary_1_2.dtd">  
      3. <taglib>  
      4.     <tlib-version>2.5</tlib-version>  
      5.     <jsp-version>1.2</jsp-version>  
      6.     <short-name>mypage</short-name>  
      7.     <uri>\tag-page</uri>  
      8.     <tag>  
      9.         <name>page</name>  
      10.         <tag-class>com.byyang.tag.PageTag</tag-class>  
      11.         <body-content>pg</body-content>  
      12.         <attribute>  
      13.             <name>url</name>  
      14.             <required>true</required>  
      15.             <rtexprvalue>true</rtexprvalue>  
      16.         </attribute>  
      17.     </tag>  
      18. </taglib>   

       

         配置web.xml

       

      Xml代碼  收藏代碼
      1.  <!-- 自定義標(biāo)簽 -->  
      2.  <jsp-config>  
      3.   <taglib>  
      4.     <taglib-uri>/tag-page</taglib-uri>  
      5.     <taglib-location>/WEB-INF/mypage.tld</taglib-location>  
      6.   </taglib>  
      7. </jsp-config>  

       

      在jsp頁面中添加如下代碼:

      Js代碼  收藏代碼
      1. <%@ taglib uri="/tag-page" prefix="pg"%>  

       

      你可以放到一個(gè)公共jsp里面一塊引。

       

      現(xiàn)在咱們可以測(cè)試一下了:

      在jsp頁面代碼:

      Html代碼  收藏代碼
      1. <div align="center">  
      2.   <h2>圖書列表</h2>  
      3.   <table border=1>  
      4.    <s:iterator var="book" status="index" value="books">  
      5.     <s:if test="#index.odd == true">  
      6.     <tr style="background-color: yellow">      
      7.     </s:if>  
      8.     <s:else>  
      9.     <tr>  
      10.     </s:else>  
      11.      <td align="right">圖書名稱:</td>  
      12.      <td align="left"><s:property value="book"/></td>  
      13.     </tr>  
      14.    </s:iterator>  
      15.   </table>  
      16.   <pg:page url="getBookList.action"></pg:page>  
      17.  </div>  

       

      Action中咱們?cè)O(shè)一個(gè)測(cè)試數(shù)據(jù):

       

      Java代碼  收藏代碼
      1. public class BookAction extends BaseAction {  
      2.    
      3.  private String[] books;  
      4.  private BookService bookService = new BookService();  
      5.    
      6.  public String execute() throws Exception {  
      7.     
      8.   setBooks(bookService.getBookList());  
      9.   Page page = new Page(1,65,5);  
      10.   request.setAttribute("pager", page);  
      11.     
      12.   return SUCCESS;  
      13.    
      14.  }  
      15.  public String[] getBooks() {  
      16.   return books;  
      17.  }  
      18.   
      19.  public void setBooks(String[] books) {  
      20.   this.books = books;  
      21.  }  
      22.   
      23. }  

       好的,接下來啟動(dòng)tomcat,查看頁面效果:

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多