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

          StringPagination類代碼為:

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

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

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

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

                
          public StringPagination() {}  
                
                
          /**  
                * 返回當前頁的字符串.  
                * 
          @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 當前頁碼數(shù).  
                 
          */
            
                 
          public int getCurrentPage() {  
                    
          if(currentPage <= 0) currentPage = 1;  

                   
          return currentPage;  
                 }
            

                 
          /**  
                  * 設(shè)置當前頁碼, 從 1 開始.  
                  *  
                  * 
          @param currentPage  
                  * The 當前頁碼數(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;                //當前頁          
                  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;                //當前頁          
                 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 一凡 閱讀(806) 評論(1)  編輯  收藏 所屬分類: JAVA 基礎(chǔ)

          評論:
          # re: JAVA字符串分頁 2009-01-08 17:32 | 那么
          寫的不錯  回復(fù)  更多評論
            
          主站蜘蛛池模板: 太和县| 额济纳旗| 甘孜| 同心县| 潼南县| 年辖:市辖区| 正定县| 永泰县| 华坪县| 社旗县| 绥棱县| 都昌县| 襄汾县| 邛崃市| 榆林市| 抚顺市| 德钦县| 鄂温| 景洪市| 富蕴县| 塘沽区| 牟定县| 兴安县| 长泰县| 体育| 辽宁省| 信阳市| 赣榆县| 临海市| 德令哈市| 探索| 当涂县| 奈曼旗| 洛浦县| 金堂县| 遂川县| 青阳县| 吉林市| 福建省| 玉环县| 宿州市|