본문 바로가기

JSP

간단한 페이징 템플릿 구현

1. 기본적으로 해당페이지에 포함되는 데이터를 가져오는 로직부분은
구현이 되어 있다고 가정합니다.

int totalSize = 300; //총 data수
int PAGEBLOCK = 10; // 1 ~ 10 페이지까지 나열
int rowPerPage 20; // 한 페이지에 보여질 row

int totalPage = (int)Math.floor((resultsize-1)/rowPerPage) + 1;
int firstPage = (int)Math.floor((Integer.parseInt(pagenum)-1)/PAGEBLOCK) * PAGEBLOCK +1;

if ( firstPage <= 0) firstPage = 1;
int lastPage = firstPage -1 + PAGEBLOCK;
if ( lastPage > totalPage ) lastPage = totalPage;


//페이징 스크립트 구현 부

<script language="javascript">
    <%if ( firstPage > PAGEBLOCK ){%>
    document.write("<a href='javascript:goPage(<%=firstPage -1%>);'>이전</a> | ");
    <%}%>
    try{
     for(i = <%=firstPage%> ; i <= <%=lastPage%> ; i++)
     {
      if(i == <%=Integer.parseInt(pagenum)%>)
       document.write("<strong>"+i+"</strong> | ");
      else
       document.write("<a href='javascript:goPage("+i+")'>"+i+"</a> | ");
     }
    }catch(e){
    }
    <%if ( lastPage < totalPage ){%>
    document.write("<a href='javascript:goPage(<%=lastPage+1%>);'>다음</a>");
    <%}%>
   </script>