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

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

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

    隨筆-204  評論-90  文章-8  trackbacks-0

    StringPagination類代碼為:

    package example;
    public class StringPagination {
          
    /** 當(dāng)前頁碼數(shù) */  
          
    private int currentPage = 1;  

          
    /** 需要分頁的長字符串 */  
          
    private String text;  

          
    /** 每頁顯示字符數(shù), 默認(rèn)為 200 */  
          
    private int sizePerPage = Constants.TEXT_PAGE_SIZE_DEFALUT;  

          
    /** 總頁數(shù) */  
          
    private int totalPage; 

          
    public StringPagination() {}  
          
          
    /**  
          * 返回當(dāng)前頁的字符串.  
          * 
    @return  
          
    */
      
          
    public String getCurrentPagedText() {  
            
    try {  
                  
    if(getCurrentPage() < getTotalPage()) {  
                    
    return getText().substring((getCurrentPage() - 1* getSizePerPage(),  
                    getCurrentPage() 
    * getSizePerPage());  
                  }
     else if(getTotalPage() > 0{  
                     
    return getText().substring((getCurrentPage() - 1* getSizePerPage());  
                  }
      
            }
     catch (Exception e) {  
                     e.printStackTrace();  
            }
      
              
    return null;  
            }
      

           
    /**  
           * 
    @return Returns the 當(dāng)前頁碼數(shù).  
           
    */
      
           
    public int getCurrentPage() {  
              
    if(currentPage <= 0) currentPage = 1;  

             
    return currentPage;  
           }
      

           
    /**  
            * 設(shè)置當(dāng)前頁碼, 從 1 開始.  
            *  
            * 
    @param currentPage  
            * The 當(dāng)前頁碼數(shù) to set.  
           
    */
      
            
    public void setCurrentPage(int currentPage) {  
               
    if (currentPage <= 0{  
                        currentPage 
    = 1;  
               }
      
                
    this.currentPage = currentPage;  
             }
      

           
    /**  
            * 
    @return Returns the 總頁碼數(shù), 如果沒有數(shù)據(jù), 就返回 1.  
           
    */
      
             
    public int getTotalPage() {  
                
    if(getText() == null) totalPage = 0;  
                  totalPage 
    = (int) Math.ceil(1.0 * getText().length() / getSizePerPage()); // 總頁面數(shù)  

                
    if (totalPage == 0)  
                         totalPage 
    = 1;  
                
    return totalPage;  
              }
      

            
    /**  
             * 
    @param totalPage  
             * The totalPage to set.  
            
    */
      
              
    public void setTotalPage(int totalPage) {  
                 
    this.totalPage = totalPage;  
              }
      

            
    /**  
             * 
    @return Returns the 每頁顯示字符數(shù).  
            
    */
      
              
    public int getSizePerPage() {  
                   
    return sizePerPage;  
               }
      

            
    /**  
             * 
    @param sizePerPage  
             * The 每頁顯示字符數(shù) to set.  
            
    */
      
              
    public void setSizePerPage(int sizePerPage) {  
                       
    this.sizePerPage = sizePerPage;  
              }
      

            
    /**  
             * 
    @return Returns the 需要分頁的長字符串.  
            
    */
      
              
    public String getText() {  
                  
    return text;  
               }
      

            
    /**  
              * 
    @param text  
              * The 需要分頁的長字符串 to set.  
            
    */
      
             
    public void setText(String text) {  
                   
    this.text = text;  
             }
      
             

               
    public static void main(String[] args) {  
                   StringPagination pager 
    = new StringPagination();  
                     pager.setSizePerPage(
    5);  
                     pager.setText(
    "12345678901");  
                     pager.setCurrentPage(
    2);  

                     System.out.println(pager.getTotalPage());  
                     System.out.println(pager.getCurrentPagedText());  
               }
      



    }

    Action代碼為:

    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) 
    {

            
    // TODO Auto-generated method stub
            StringPagination stringPagination = new StringPagination();
            WapResource wapResource 
    = new WapResource();
            
            
    int curPage;                //當(dāng)前頁          
            int maxPage;                // 總頁數(shù) 
            String strResPic = "";
            String content 
    = "";
            String resName 
    = "";
            String outputContent 
    = "";
            
            String strResId 
    = request.getParameter("resId");
            String strPageNo 
    = request.getParameter("curPage");
             
            
    try {
                content 
    = new String(request.getParameter("content").getBytes("ISO-8859-1"),"UTF-8");
                            
            }
     catch (UnsupportedEncodingException e) {
                
    // TODO Auto-generated catch block
                e.printStackTrace();
            }

            
            
    if(strPageNo != null){
                
    if(!"1".equals(strPageNo)){
                    stringPagination.setCurrentPage(Integer.parseInt(strPageNo));
                }

            }

            
            List wapResourceList 
    = this.getWapResourceDao().getWapResourceByResID(Long.valueOf(strResId));
            
            
            
    if(wapResourceList != null){
    //            String pagination process
                wapResource = (WapResource)wapResourceList.get(0);
                curPage 
    = stringPagination.getCurrentPage();            
                strResPic 
    = wapResource.getResPic();
                resName 
    = wapResource.getResName();            
                stringPagination.setText(wapResource.getResContent());       
                maxPage 
    = stringPagination.getTotalPage();
                outputContent 
    = stringPagination.getCurrentPagedText();
                
                request.setAttribute(
    "wapResourceList", wapResourceList);
                request.setAttribute(
    "content", content);
                
                request.setAttribute(
    "curPage", String.valueOf(curPage));
                request.setAttribute(
    "forward", String.valueOf(stringPagination.getCurrentPage() - 1));
                request.setAttribute(
    "back", String.valueOf(stringPagination.getCurrentPage() + 1));
                request.setAttribute(
    "maxPage", String.valueOf(maxPage));
                request.setAttribute(
    "strResPic", strResPic);
                request.setAttribute(
    "resName", resName);
                request.setAttribute(
    "strResId", strResId);
                request.setAttribute(
    "outputContent", outputContent);
                
                
            }

            
    return mapping.findForward("searchContent");
        
        }

    JSP 頁面:
    <%@ page contentType="text/vnd.wap.wml; charset=UTF-8"%>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

    <?xml version="1.0"?>
    <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
    <wml>
    <head>
        
    <meta http-equiv="Cache-Control" content="no-cache" />
        
    <meta http-equiv="Cache-Control" content="max-age=0" />
    </head>

    <card title='<bean:message key="prompt.index.title"/>'>

    <p align='left'>
        
    <%
           
    int curPage,forward, back;                //當(dāng)前頁          
           int maxPage;                // 總頁數(shù) 
           String content = (String)request.getAttribute("content");
           String strResPic 
    = (String)request.getAttribute("strResPic");
           String outputContent 
    = (String)request.getAttribute("outputContent");
           String resName 
    = (String)request.getAttribute("resName");
           String resId 
    = (String)request.getAttribute("strResId");
           curPage 
    = Integer.parseInt(request.getAttribute("curPage").toString());
           maxPage 
    = Integer.parseInt(request.getAttribute("maxPage").toString());
           forward 
    = Integer.parseInt(request.getAttribute("forward").toString());
           back 
    = Integer.parseInt(request.getAttribute("back").toString());
               
    if(null != strResPic){
        
    %>
                   
    <img src='<%=strResPic%>' alt='img' />
                   
    <br/>
        
    <%
                 }

                 out.print(resName);
                 out.print(
    "<br/><br/>");             
                 out.print(outputContent);
                 
        
    %>
        
    <br/>
        
        
    <%             
                   
    if (curPage != 1  &&  maxPage > 0 ) {
         
    %>     
           
    <anchor>上一頁
              
    <go href="/cars/searchContent.do" method="post" accept-charset="UTF-8"> 
                 
    <postfield name="curPage" value="<%=forward%>"/>
                 
    <postfield name="resId" value="<%=resId%>"/> 
                 
    <postfield name="content" value="<%=content%>"/>              
              
    </go> 
           
    </anchor>
           
    <br/>
                  
    <%
                   }
    if (curPage != maxPage  &&  maxPage > 0 ) {
                 
    %>
                   
            
    <anchor>下一頁
              
    <go href="/cars/searchContent.do" method="post" accept-charset="UTF-8"> 
                 
    <postfield name="curPage" value="<%=back%>"/> 
                 
    <postfield name="resId" value="<%=resId%>"/> 
                 
    <postfield name="content" value="<%=content%>"/>              
              
    </go> 
           
    </anchor>
            
    <br/>
            
            
    <%
                  }

            
    %>    
         共
    <%=maxPage%>
         
    <br/>
            
        
    <anchor> 返回上級
              
    <go href="/cars/search.do" method="post" accept-charset="UTF-8"> 
                 
    <postfield name="content" value="<%=content%>"/> 
              
    </go> 
        
    </anchor>
           
        
    <br/>                                      
        
    <%@include file="../bottom1_1.jsp"%>    

    </p>

    </card>

    </wml>

    很簡單的,大家看看吧,我也參考了別人的代碼……
    posted on 2006-02-28 18:37 一凡 閱讀(802) 評論(1)  編輯  收藏 所屬分類: JAVA 基礎(chǔ)

    評論:
    # re: JAVA字符串分頁 2009-01-08 17:32 | 那么
    寫的不錯  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲熟女少妇一区二区| 亚洲精品少妇30p| 久久久久久亚洲精品| 亚洲精品伊人久久久久| 精品国产污污免费网站入口在线| 亚洲w码欧洲s码免费| 亚洲国产综合久久天堂| 亚洲国产美女福利直播秀一区二区| 黄色三级三级三级免费看| 91av免费观看| 亚洲无线一二三四区手机| 77777午夜亚洲| 叮咚影视在线观看免费完整版| 在线免费观看色片| 亚洲va中文字幕无码久久不卡| 亚洲AV成人一区二区三区观看 | 一区在线免费观看| 日本亚洲免费无线码| 亚洲人成网站在线播放vr| 亚洲高清毛片一区二区| 4虎1515hh永久免费| 国产亚洲精品激情都市| 亚洲精品女同中文字幕| 亚洲一级免费毛片| 精品久久久久久亚洲| 国产综合成人亚洲区| 日本成年免费网站| 亚洲AV无码成人精品区在线观看| 国产在亚洲线视频观看| 成年免费大片黄在线观看岛国| 亚洲成av人片天堂网| 九九综合VA免费看| 欧洲美熟女乱又伦免费视频| 亚洲成人免费电影| 久久免费观看国产精品| 国产亚洲一区区二区在线| 国产精品久久久久久亚洲小说| 成年人网站在线免费观看| 亚洲日韩中文字幕天堂不卡| 在线观看免费无码专区 | 亚洲精品第一国产综合境外资源|