페이징처리 예제 - 동적쿼리, include taglib 이용 :: 소림사의 홍반장!

PageListTag.java

package kr.co.khi.common;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

 

public class PageListTag extends TagSupport {

      private int page;

      private int firstPage;

      private int lastPage;

      private int totPage;

      private String pageLinkUrl;

      private boolean method;

     

      @Override

      public int doEndTag() throws JspException {

            StringBuilder pageTag = new StringBuilder();

            if (!method) {

                  pageTag.append("<div class=\"paginate\">");

                  if (firstPage != 1)

                        pageTag.append("<a href=\"" + pageLinkUrl + "&amp;page="

                                   + (firstPage - 1) + "\" class=\"prev\">이전</a>");

                  for (int i = firstPage; i < lastPage + 1; i++) {

                        // 현재 페이지는 <strong>으로 감싸서 표현

                        if (page == i)

                             pageTag.append("      <a href=\"" + pageLinkUrl + "&amp;page="

                                         + i + "\"><strong>" + i + "</strong></a>");

                        else

                             pageTag.append("      <a href=\"" + pageLinkUrl + "&amp;page="

                                         + i + "\">" + i + "</a>");

                  }

                  if (lastPage < totPage)

                        pageTag.append("<a href=\"" + pageLinkUrl + "&amp;page="

                                   + (lastPage + 1) + "\" class=\"next\">다음</a>");

                  pageTag.append("</div>");

            } else {

                  pageTag.append("<div class=\"paginate\">");

                  if (firstPage != 1)

                        pageTag.append("<a href=\"" + pageLinkUrl + "("

                                   + (firstPage - 1) + ");\" class=\"prev\">이전</a>");

                  for (int i = firstPage; i < lastPage + 1; i++) {

                        // 현재 페이지는 <strong>으로 감싸서 표현

                        if (page == i)

                             pageTag.append("      <a href=\"" + pageLinkUrl + "("

                                         + i + ");\"><strong>" + i + "</strong></a>");

                        else

                             pageTag.append("      <a href=\"" + pageLinkUrl + "("

                                         + i + ");\">" + i + "</a>");

                  }

                  if (lastPage < totPage)

                        pageTag.append("<a href=\"" + pageLinkUrl + "("

                                   + (lastPage + 1) + ");\" class=\"next\">다음</a>");

                  pageTag.append("</div>");

            }

            try {

                  pageContext.getOut().write(pageTag.toString());

            } catch (IOException e) {

                  e.printStackTrace();

            }

            return EVAL_PAGE;

      }

     

      public void setPage(int page) {

            this.page = page;

      }

      public void setFirstPage(int firstPage) {

            this.firstPage = firstPage;

      }

      public void setLastPage(int lastPage) {

            this.lastPage = lastPage;

      }

      public void setTotPage(int totPage) {

            this.totPage = totPage;

      }

      public void setPageLinkUrl(String pageLinkUrl) {

            this.pageLinkUrl = pageLinkUrl;

      }

      public void setMethod(boolean method) {

            this.method = method;

      }

}

 

 

page-taglib.tld

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE taglib

        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"

      "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">

     

<taglib>

  <tlib-version>1.1</tlib-version>

  <jsp-version>1.2</jsp-version>

  <short-name>pageList</short-name>

  <description>page list tag</description>

  <tag>

    <name>page</name>

    <tag-class> kr.co.khi.common.PageListTag</tag-class>

    <body-content>EMPTY</body-content>

    <description>

      페이지 보여주는 태그

    </description>  

    <attribute>

        <name>pageLinkUrl</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

        <type>java.lang.String</type>

    </attribute>

    <attribute>

        <name>page</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

        <type>int</type>

    </attribute>

    <attribute>

        <name>totPage</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

        <type>int</type>

    </attribute>

    <attribute>

        <name>firstPage</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

        <type>int</type>

    </attribute>

    <attribute>

        <name>lastPage</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

        <type>int</type>

    </attribute>

    <attribute>

        <name>method</name>

        <required>false</required>

        <rtexprvalue>true</rtexprvalue>

        <type>boolean</type>

    </attribute>

  </tag>

</taglib>

 

 

page_inc.jsp

<?xml version="1.0" encoding="UTF-8" ?>

<%-- <%@ page language="java" contentType="text/html; charset=utf-8" --%>

<%--     pageEncoding="utf-8"%> --%>

<%@ taglib prefix="pageList"

      uri="/WEB-INF/page-taglib.tld" %>

<style type="text/css">

.paginate {

    line-height: 10px;

    margin: 10px 0 10px;

    text-align: center;

    width: 100%;

}

.paginate a.next {

    border-right: 0 none;

    font-weight: normal;

    margin-right: -4px;

    padding-right: 13px;

}

.paginate a.prev {

    border-left: 0 none;

    font-weight: normal;

    margin-left: -4px;

    padding-left: 13px;

}

.paginate a{

    background: none repeat scroll 0 0 #FFFFFF;

    border-left: 1px solid #CCCCCC;

    border-right: 1px solid #CCCCCC;

    color: #333333;

    display: inline-block;

    font: bold 12px/11px "돋움",dotum;

    margin-right: -5px;

    overflow: hidden;

    padding: 2px 7px 0;

    text-decoration: none !important;

    vertical-align: middle;

}

.paginate strong{

      color: #FF8400;

}

</style>

<pageList:page

 pageLinkUrl="javascript:doList"

 totPage="${totPage }" lastPage="${lastPage }"

 page="${page }" firstPage="${firstPage }"

                  method="true"/>

 

 

 

view 페이지에서의 사용

      <%@ include file="/inc/page_inc.jsp" %>

 

 

 

Controller에서의 사용

@RequestMapping(value="/moccoji", method={RequestMethod.GET,RequestMethod.POST})

    public String moccojiHome(HttpSession session, MoccojiDto dto, ModelMap map) {

          String userid = (String)session.getAttribute("userid");

         

        int page = 1;           // 현재 페이지

        int pageSize = 7;       // 페이지당 글 개수

        int firstRow = 0;       // 보여질 첫번째 글순서

        int lastRow = 0;        // 보여질 마지막 글순서

        int totSize = 0;        // 전체 글 개수

        int totPage = 0;        // 전체 페이지수

        int pageListSize = 3;   // 페이징에서 보여줄 페이지 개수

        int firstPage = 0;      // 보여질 첫번째 페이지

        int lastPage = 0;       // 보여질 마지막 페이지

       

        String strPage = dto.getPage();

 

        if(strPage != null && !"".equals(strPage))

            page = Integer.parseInt(strPage);

       

        lastRow = page * pageSize;

        firstRow = lastRow - (pageSize - 1);

       

        dto.setFirstRow(firstRow);

        dto.setLastRow(lastRow);

       

        totSize = moccDao.getTotSize(dto);

        totPage = (totSize-1)/pageSize + 1 

        firstPage = page - (page-1)%pageListSize;

        lastPage = firstPage + (pageListSize-1);

        if(totPage < lastPage)

            lastPage = totPage; // 전체(5) last(6) -> last = 5

         

          // 내 모꼬지 가져오기

          List<MoccojiDto> myList;

        if (userid != null) {

            System.out.println("내모꼬지 가져오기");

            myList = moccDao.getMyList(userid);

            map.addAttribute("myList",myList);

        }

       

        List<MoccojiDto> allList;

        String search = dto.getSearch();

        if(search == null || search.equals("")) {

            // 전체 모꼬지 가져오기

            allList = moccDao.getAllList(dto);

        } else {

            // 검색 모꼬지 가져오기

            allList = moccDao.getSearchList(dto);

        }

        map.addAttribute("allList",allList);

        map.addAttribute("page", page);

        map.addAttribute("totPage", totPage);

        map.addAttribute("firstPage", firstPage);

        map.addAttribute("lastPage", lastPage);

         

          return ".moccoji.mocc_home";

      }

 

 

Model - DAO에서의 사용

    public int getTotSize(MoccojiDto dto) {

        return mapper.selectOne("moccoji.getTotSize", dto);

    }

 

    public List<MoccojiDto> getAllList(MoccojiDto dto) {

          return mapper.selectList("moccoji.getAllList",dto);

      }

 

 

사용되는 쿼리문

<select id="getTotSize" resultType="int"

            parameterType="kr.co.khi.dto.MoccojiDto">

      select count(*) from moccoji

</select>

 

 

<!-- 모든 모꼬지 정보 -->

<select id="getAllList" parameterType="kr.co.khi.dto.MoccojiDto" resultType="kr.co.khi.dto.MoccojiDto">

      select *

      from (select row_number() over (order by end_date desc) r,

        gender_type,order_status,name,description,location,

        member_count,start_date,end_date,period,img_addr,moccoji_code

        from(

        select distinct

          sc1.remark gender_type, nvl(sc3.remark,'예약없음') order_status, m.name,

          m.description, sc.remark location, sc2.remark member_count,

          to_char(m.start_date,'yyyy-mm-dd') start_date, to_char(m.end_date,'yyyy-mm-dd') end_date,

          (m.end_date-m.start_date)||''||(m.end_date-m.start_date+1)||'' period,

          m.img_addr, mj.moccoji_code

        from moccoji_join mj

          left join moccoji m on m.moccoji_code = mj.moccoji_code

          left join package_order po on m.moccoji_code = po.moccoji_code

          left join subcode sc on m.location = sc.subcode

          left join subcode sc1 on m.gender_type = sc1.subcode

          left join subcode sc2 on m.member_count = sc2.subcode

          left join subcode sc3 on po.order_status = sc3.subcode

        where sc.maincode='A02'

          and sc1.maincode='U04'

          and sc2.maincode='A01'

          and (sc3.maincode='B01'

          or order_status is null)

          )

      order by end_date desc)

      where r between #{firstRow} and #{lastRow}

</select>

 

 

 

 

 

다른 카테고리의 글 목록

Dev. 스프링/참고소스 및 예제 카테고리의 포스트를 톺아봅니다