정미나닷컴

[Java] 자바 페이징 함수 본문

IT

[Java] 자바 페이징 함수

정미나 2010. 10. 5. 14:46
public String getPaging(int totalCount, int currentPage, int pageSize, int pageGroupSize){

   String pageStr = "";
  
   int pageGroupCount = totalCount / (pageSize * pageGroupSize) + (totalCount % (pageSize * pageGroupSize) == 0 ? 0 :1);
   int numPageGroup = (int)Math.ceil((double)currentPage / pageGroupSize); 
  
   if (totalCount > 0){
      int pageCount = (totalCount / pageSize) + (totalCount % pageSize == 0 ? 0 : 1);
      int startPage = pageGroupSize * (numPageGroup - 1) + 1;
      int endPage = startPage + pageGroupSize-1;
   
      String preImg = "<img alt=\"이전으로\" src=\"이전 이미지\" onfocus=\"this.blur();\" style=\"cursor:pointer\">";
      String nextImg = "<img alt=\"다음으로\" src=\"다음 이미지\" onfocus=\"this.blur();\" style=\"cursor:pointer\">";
   
      if (endPage > pageCount ){
         endPage = pageCount;
      }
    
      if (numPageGroup > 1){
         pageStr += "<a onclick=\"goPage(" + ((numPageGroup-2) * pageGroupSize + 1) + ")\"  onfocus=\"this.blur();\" style=\"cursor:pointer\">" + preImg + "</a> ";
      }
   
      for (int i = startPage; i <= endPage; i++){

         if (currentPage == i){
            pageStr += "<a onclick=\"goPage(" + i + ")\" class=\"thisPage\"  onfocus=\"this.blur();\" style=\"cursor:pointer\">" + i + "</a> ";
         }else{
            pageStr += "<a onclick=\"goPage(" + i + ")\"  onfocus=\"this.blur();\" style=\"cursor:pointer\">" + i + "</a> ";
         }
      }
   
      if (numPageGroup < pageGroupCount){
         pageStr += "<a onclick=\"goPage(" + (numPageGroup * pageGroupSize + 1) + ")\"  onfocus=\"this.blur();\" style=\"cursor:pointer\">" + nextImg + "</a> ";
      }
   }
   return pageStr;
}


int sCurrentPage = 0;                // 현재페이지 번호
int sRowCntPerPage = 10;         // 페이지당 게시글 수
int sStartRow  = 0;                    // 조회시 첫 인덱스
int sEndRow   = 0;                   // 조회시 마지막 인덱스
int sTotalCount  = 100;              // 총데이타 건수

if(request.getParameter("pageNo")!= null) sCurrentPage = Integer.parseInt(request.getParameter("pageNo"));
else sCurrentPage = 1;

sStartRow = (sCurrentPage - 1) * sRowCntPerPage + 1;
sEndRow   = sCurrentPage * sRowCntPerPage;

// 현재페이지번호 > 1 && 현재페이지번호 * 페이지당Row > 총건수 + 페이지당 Row -1
if( sCurrentPage > 1 && ( sCurrentPage * sRowCntPerPage ) > sTotalCount + sRowCntPerPage - 1){
   sCurrentPage = sCurrentPage - 1;
}


<%=getPaging(sTotalCount, sCurrentPage, sRowCntPerPage, 5)%>