본문 바로가기

카테고리 없음

학원꺼 페이징 방법

728x90

리스트

@RequestMapping("/ConsultationLists.mvc")
public String consultationLists(HttpServletRequest request) {

	String pageNum = request.getParameter("pageNum");
	if(pageNum == null)
	{
		pageNum="1";
	}

	String pageUrl ="[1]";

	int recordCountPerPage = 10; //한 페이지당 게시물 수
	int start =((Integer.parseInt(pageNum)-1) * recordCountPerPage)+1;
	int end = Integer.parseInt(pageNum) * recordCountPerPage;

	int count = 0;		

	Map map = new HashMap();

	map.put("start", start);
	map.put("end", end);

	List lists = null;

	try {
		lists = dao.getListData("consultation.lists", map);
		count = dao.getIntValue("consultation.count");
		
		int pageCount = util.getPageCount(recordCountPerPage, count);
		pageUrl = util.pageIndexList(Integer.parseInt(pageNum), pageCount, "ConsultationLists.mvc");
		
	} catch (Exception e) {
		e.printStackTrace();
	}

	request.setAttribute("lists", lists);
	request.setAttribute("pageUrl", pageUrl);

	return "ConsultationLists.jsp";
}

SQL : consultation.lists

select rnum, cid, name, id, title, wdate, ipaddr, replyCount, commentCount from consultationViewPage 
where rnum >= #start# and rnum <= #end#

페이징

public String pageIndexList(int current_page, int total_page, String list_url) {

	int numPerBlock = 10;   // 리스트에 나타낼 페이지 수
	int currentPageSetUp;
	int n;
	int page;
	StringBuffer strList = new StringBuffer();

	if(current_page == 0)
		return "";

	if(list_url.indexOf("?") != -1)
		list_url = list_url + "&";
	else
		list_url = list_url +"?";

	// 표시할 첫 페이지
	currentPageSetUp = (current_page / numPerBlock) * numPerBlock;
	if (current_page % numPerBlock == 0)
	    currentPageSetUp = currentPageSetUp - numPerBlock;

	// 1 페이지
	if ((total_page > numPerBlock) && (currentPageSetUp > 0)) {
	   strList.append("1 ");
	}

	// [Prev] : 총 페이지수가 numPerBlock 이상인 경우 이전 numPerBlock 보여줌
	n = current_page - numPerBlock;
	if (total_page > numPerBlock && currentPageSetUp > 0) {
	   strList.append("[Prev] ");
	}

	// 바로가기 페이지 구현
	page = currentPageSetUp + 1;
	while((page <= total_page) && (page <= currentPageSetUp + numPerBlock)) {
	   if(page == current_page) {
	     strList.append(""+page+" ");
	   }
	   else {
	     strList.append(""+page+" ");
	   }
	   page++;
	}

	// [Next] : 총 페이지수가 numPerBlock 페이지 이상인 경우 다음 numPerBlock 페이지를 보여줌
	// n = currentPageSetUp + numPerBlock + 1;
	n = current_page + numPerBlock;
	if (total_page - currentPageSetUp > numPerBlock) {
			strList.append("[Next] ");
	}

	// 마지막 페이지
	if ((total_page > numPerBlock) && (currentPageSetUp + numPerBlock < total_page)) {
			strList.append(""+total_page+"");
	}

	return strList.toString();
}


728x90