본문 바로가기

비트교육센터[전문가반]

Paging 처리 [Spring]

[Controller]

1. View에서 필요한 전체 게시판 글의 개수를 가져온다. 

=>   int totalCnt = oneToOneService.findAllCnt(authUser.getId());

 

2. 첫 페이지는 page=1로 잡아주고 page=1에 해당하는 게시판의 글을 DB에 조회해서 가져온다.

(여기서는 해당 작성자에 대한 글을 가져오기때문에 page와 작성자의 id 값을 매개변수로 넘겨주었다.)

=>  List<OneToOneVo> list = oneToOneService.findAll(page, authUser.getName());

 

 

@Autowired

     private OneToOneService oneToOneService;

 

@RequestMapping("")

     public String index(HttpSession session, Model model) {

           UserVo authUser = (UserVo) session.getAttribute("authUser");

           int totalCnt = oneToOneService.findAllCnt(authUser.getId());

          

           int page = 1;

           List<OneToOneVo> list = oneToOneService.findAll(page, authUser.getName());

           model.addAttribute("list", list);

           model.addAttribute("p", page);

           model.addAttribute("totalCnt", totalCnt);

          

           return "oneToOne/index";

     }

 

     @RequestMapping(value = "/{page}")

     public String index(HttpSession session, @PathVariable("page") int page, Model model) {

           UserVo authUser = (UserVo) session.getAttribute("authUser");

           int totalCnt = oneToOneService.findAllCnt(authUser.getId());

          

           List<OneToOneVo> list = oneToOneService.findAll(page, authUser.getName());

           model.addAttribute("list", list);

           model.addAttribute("p", page);

           model.addAttribute("totalCnt", totalCnt);

          

           return "oneToOne/index";

     }

[Service]

@Autowired

private OneToOneRepository oneToOneRepository;

 

//글쓴이가 작성한 게시판 전체 개수

public int findAllCnt(String nickname) {

     

     return oneToOneRepository.findAllCnt(nickname);

}

    

//해당 페이지에 나타낼 게시판의 리스트 가져오기

public List<OneToOneVo> findAll(int page, String name) {

 

     return oneToOneRepository.findAll(page, name);

}

 

[Repository]

@Autowired

private SqlSession sqlSession;

 

//글쓴이가 작성한 게시판 전체 개수

public int findAllCnt(String nickname) {
     return sqlSession.selectOne("onetoone.findAllCnt", nickname);
}

 

//해당 페이지에 게시물을 10개씩 뽑아오기위해 start, end 변수를 생성

public List<OneToOneVo> findAll(int page, String name) {

     int start = 1 + (page-1) * 10; //1, 11, 21, 31

     int end = page * 10; //10, 20, 30, 40

     Map params = new HashMap();

     params.put("start", start);

     params.put("end", end);

     params.put("name", name);

          

     return sqlSession.selectList("onetoone.findAll", params);

}

 

 

[게시판 SQL정보가 있는 xml파일]

<select id="findAll" parameterType="map" resultType="onetoonevo">

           <![CDATA[

                select N2.no, N2.title, N2.content, N2.writer, N2.regdate from

                     ( select @ROWNUM:=@ROWNUM+1 num, N1.*

                           from (

                                     select *

                                from onetoone

                                order by regdate desc

                                ) as N1

                           WHERE (@rownum:=0)=0 and writer= #{name }

                     ) as N2

                where num between #{start } and #{end }

           ]]>

     </select>

    

     <select id="findAllCnt" parameterType="String" resultType="int">

           <![CDATA[

                select count(*)

                from onetoone

                where writer = #{nickname }

           ]]>

     </select>

 

 

 

[index.jsp]

<!-- pager 추가 -->

 

                     <!-- 현재 page 시작 page, 마지막 page 값을 구해서 각각의 변수에 담았다. -->

                     <!-- ****************************************************** -->

                     <c:set var="page" value="${(empty p)?1:p}" />

                     <c:set var="startNum" value="${page-(page-1)%5}" />

                     <c:set var="lastNum" value="${fn:substringBefore(Math.ceil(count/10), '.') }" />

                     <!-- pager 버튼 개수가 1부터4로 하고싶은데 1부터 4까지 있으려면 40개의 게시물이어야한다.

                           40개의 게시물이 아닐수있으니까 아래의 삼항연산을 통해 구별한다.-->

                   <c:set var="end" value="${totalCnt/10 > 4 ? 4:totalCnt/10 }" />  

                     <!-- ****************************************************** -->

                     <div class="pager">

                           <ul>

                                <li>

                                <c:choose>

                                     <c:when test="${startNum > 1}">

                                          <a href="${pageContext.request.contextPath }/oneToOne/${startNum-1}" ></a>

                                     </c:when>

                                     <c:otherwise>

                                          <span onclick="alert('이전 페이지가 없습니다.');"></span>

                                     </c:otherwise>

                                </c:choose>

                                </li>

                                <c:forEach var="i" begin="0" end="${end }">

                                     <li>

                                          <a href="${pageContext.request.contextPath  }/oneToOne/${startNum+i}">${startNum+i}</a>

                                     </li>

                                </c:forEach>

                                <li>

                                     <c:choose>

                                          <c:when test="${startNum+4<lastNum}">

                                                <a href="${pageContext.request.contextPath }/oneToOne/${startNum+i}"></a>

                                          </c:when>

                                          <c:otherwise>

                                                <span onclick="alert('다음 페이지가 없습니다.');"></span>

                                          </c:otherwise>

                                     </c:choose>

                                </li>

                               

                           </ul>

                     </div>

 

 

<!-- pager 추가 -->