게시판 페이징
여러 페이징 방법들이 있는 것 같다.
query
<!-- 게시물 Count, YN은 삭제 유무 -->
<select id="selectListcount" parameterClass="noticeBoard" resultClass="int">
SELECT COUNT(*) CNT
FROM NOTICE_BOARD
WHERE USE_YN = 'Y'
ORDER BY SEQ DESC
</select>
<!-- 공지사항 리스트 -->
<select id="selectList" resultClass="noticeBoard">
SELECT *
FROM (SELECT ROWNUM AS RNUM,
A.*
FROM (SELECT SEQ,
ACCOUNTID,
NAME,
TITLE,
CONTENTS,
IMAGE_PATH,
IMAGE_NAME,
ATTACH_PATH,
ATTACH_NAME,
VIEWCOUNT,
CREATE_DATE,
USE_YN,
UPDATE_ACCOUNTID,
UPDATE_DATA
FROM NOTICE_BOARD
WHERE USE_YN = 'Y'
ORDER BY SEQ DESC ) A ) B
WHERE B.RNUM BETWEEN #startNum# AND #endNum#
</select>
BaseObject.java
package web.common.dto;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import java.io.Serializable;
public class BaseObject implements Serializable {
private static final long serialVersionUID = -6595166884231650472L;
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}
ListObject.java (게시판 VO는 ListObject.java를 상속받는다.)
package web.common.dto;
public class ListObject extends BaseObject{
private static final long serialVersionUID = 4620091055889814987L;
private String sortType;
private int count = 0;
private int pageNo = 1;
private int pageSize = 10;
public String getSortType() {
return this.sortType;
}
public void setSortType(String sortType) {
this.sortType = sortType;
}
public int getCount() {
return this.count;
}
public void setCount(int count) {
this.count = count;
}
public int getPageNo() {
return this.pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return this.pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStartNum() {
int pageSize = getPageSize();
return getPageNo() * pageSize - pageSize + 1;
}
public int getEndNum() {
return getPageNo() * getPageSize();
}
}
Service.java (메서드)
public ResultDTO<NoticeBoard> selectListForPaging(NoticeBoard noticeBoard) throws Exception {
ResultDTO<NoticeBoard> resultDTO = new ResultDTO<NoticeBoard>();
int totalCount = adminNoticeDao.selectListCount(noticeBoard);
List<NoticeBoard> list = adminNoticeDao.selectList(noticeBoard);
Paging paging = new Paging(noticeBoard.getPageNo(),totalCount,noticeBoard.getPageSize(),10);
resultDTO.setList(list);
resultDTO.setCount(totalCount);
resultDTO.setPaging(paging);
return resultDTO;
}
Controller.java (메서드)
@RequestMapping(value="/admin/notice/list")
public String list(HttpServletRequest request, HttpServletResponse response,
@RequestParam HashMap<String, Object> params ,
NoticeBoard noticeBoard ,
ModelMap modelMap) throws Exception{
try {
log.debug("noticeBoard => {}",noticeBoard);
//modelMap.addAttribute("listVO", adminNoticeService.selectList(noticeBoard));
ResultDTO<NoticeBoard> resultDTO = adminNoticeService.selectListForPaging(noticeBoard);
modelMap.addAttribute("listVO", resultDTO);
log.debug("ResultDTO => {}",resultDTO);
} catch (SQLException e) {
System.out.println(e);
}
return "admin/notice/list";
}
Paging.java
package web.common.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* The type Paging.
*/
public class Paging implements Serializable {
private static final long serialVersionUID = 1952275543463278952L;
private int curPage;
private int curPrevPage;
private int curNextPage;
private int scale;
private int totalLastPage;
private int viewLastPage;
private int viewFirstPage;
private int nextPage;
private int prePage;
private int totalCount;
private List<Integer> pageList;
/**
* Instantiates a new Paging.
*/
public Paging() {
}
/**
* Instantiates a new Paging.
*
* @param curPage the cur page
* @param totalCount the total count
* @param listScale the list scale
* @param pageScale the page scale
*/
public Paging(int curPage, int totalCount, int listScale, int pageScale) {
this.curPage = curPage;
this.setTotalCount(totalCount);
this.setScale(listScale);
if (totalCount <= listScale) {
this.totalLastPage = 1;
} else {
this.totalLastPage = (int) Math.ceil((double) totalCount
/ listScale);
}
if (this.totalLastPage < this.curPage) {
this.curPage = 1;
}
this.prePage = (this.curPage - 1) / pageScale * pageScale;
if (this.prePage == 0) {
this.prePage = 1;
}
if(this.curPage -1 == 0){
this.curPrevPage = 1;
}else{
this.curPrevPage = this.curPage -1;
}
this.viewLastPage = ((this.curPage - 1) / pageScale + 1) * pageScale;
this.viewFirstPage = viewLastPage - (pageScale - 1);
if (this.totalLastPage <= pageScale) {
this.viewLastPage = this.totalLastPage;
this.nextPage = this.viewLastPage + 1;
} else {
if (this.viewLastPage > this.totalLastPage) {
this.viewLastPage = this.totalLastPage;
this.nextPage = 0;
} else {
this.nextPage = this.viewLastPage + 1;
}
}
if(this.curPage +1 > this.totalLastPage){
this.curNextPage = this.totalLastPage;
}else{
this.curNextPage = this.curPage+1;
}
List<Integer> page = new ArrayList<Integer>();
for (int i = viewFirstPage; i <= viewLastPage; i++) {
page.add(i);
}
this.setPageList(page);
}
/**
* Gets cur page.
*
* @return the cur page
*/
public int getCurPage() {
return curPage;
}
/**
* Sets cur page.
*
* @param curPage the cur page
*/
public void setCurPage(int curPage) {
this.curPage = curPage;
}
/**
* Gets total last page.
*
* @return the total last page
*/
public int getTotalLastPage() {
return totalLastPage;
}
/**
* Sets total last page.
*
* @param totalLastPage the total last page
*/
public void setTotalLastPage(int totalLastPage) {
this.totalLastPage = totalLastPage;
}
/**
* Gets view last page.
*
* @return the view last page
*/
public int getViewLastPage() {
return viewLastPage;
}
/**
* Sets view last page.
*
* @param viewLastPage the view last page
*/
public void setViewLastPage(int viewLastPage) {
this.viewLastPage = viewLastPage;
}
/**
* Gets next page.
*
* @return the next page
*/
public int getNextPage() {
return nextPage;
}
/**
* Sets next page.
*
* @param nextPage the next page
*/
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
/**
* Gets pre page.
*
* @return the pre page
*/
public int getPrePage() {
return prePage;
}
/**
* Sets pre page.
*
* @param prePage the pre page
*/
public void setPrePage(int prePage) {
this.prePage = prePage;
}
/**
* Sets page list.
*
* @param pageList the page list
*/
public void setPageList(List<Integer> pageList) {
this.pageList = pageList;
}
/**
* Gets page list.
*
* @return the page list
*/
public List<Integer> getPageList() {
return pageList;
}
/**
* Sets scale.
*
* @param scale the scale
*/
public void setScale(int scale) {
this.scale = scale;
}
/**
* Gets scale.
*
* @return the scale
*/
public int getScale() {
return scale;
}
/**
* Sets total count.
*
* @param totalCount the total count
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/**
* Gets total count.
*
* @return the total count
*/
public int getTotalCount() {
return totalCount;
}
/**
* Gets view first page.
*
* @return the view first page
*/
public int getViewFirstPage() {
return viewFirstPage;
}
/**
* Sets view first page.
*
* @param viewFirstPage the view first page
*/
public void setViewFirstPage(int viewFirstPage) {
this.viewFirstPage = viewFirstPage;
}
/**
* Gets cur prev page.
*
* @return the cur prev page
*/
public int getCurPrevPage() {
return curPrevPage;
}
/**
* Sets cur prev page.
*
* @param curPrePage the cur pre page
*/
public void setCurPrevPage(int curPrePage) {
this.curPrevPage = curPrePage;
}
/**
* Gets cur next page.
*
* @return the cur next page
*/
public int getCurNextPage() {
return curNextPage;
}
/**
* Sets cur next page.
*
* @param curNextPage the cur next page
*/
public void setCurNextPage(int curNextPage) {
this.curNextPage = curNextPage;
}
}
PagingUtils.java
package web.common.util;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
public class PagingUtil {
public static String getPageNavigation(int page, int totalCount, String url, String linkString, int pageSize, int pagePerPageSize) {
StringBuffer result = new StringBuffer();
int totalPageSize = 0;
if(pageSize > 0 && totalCount > 0) {
linkString = StringUtils.isBlank(linkString) ? "?" : "?"+linkString+"&";
int startPage = 1;
if(totalCount != 0) {
totalPageSize = (totalCount % pageSize > 0) ? 1 : 0;
totalPageSize = totalPageSize + totalCount / pageSize;
startPage = (page - 1) / pagePerPageSize * pagePerPageSize + 1;
}
int prevPage = (page - 1);
int nextPage = (page + 1);
if (page == totalCount)
{
nextPage = totalCount;
}
if (prevPage == 0)
{
prevPage = 1;
}
int endPage = ((startPage + pagePerPageSize) - 1 > totalPageSize) ? totalPageSize : (startPage + pagePerPageSize)-1;
//처음
if(1 < page) {
result.append("\n <span class=\"btn_p prev_e\"><a href=\"" + url + linkString + "pageNo=1\" alt=\"처음페이지\"><<</a></span>\n");
} else {
result.append("\n <span class=\"btn_p prev_e\"><a href=\"#\" onclick=\"return false;\" alt=\"처음페이지\"><<</a></span>\n");
}
//이전페이지 그룹
if(0 < prevPage) {
result.append("\n <span class=\"btn_p prev\"><a href=\"" + url + linkString + "pageNo=" + prevPage + "\" alt=\"이전\"><</a></span>\n");
} else {
result.append("\n <span class=\"btn_p prev\"><a href=\"" + url + linkString + "pageNo=1\" alt=\"이전\"><</a></span>\n");
}
for(int i=startPage; i <= endPage ; i++) {
if(i == page) { // 현재페이지
result.append("\n <span class=\"btn_num on\"><a href=\""+url+linkString+"pageNo="+i+"\" >"+i+"</a></span>\n");
} else {
result.append("\n <span class=\"btn_num\"><a href=\""+url+linkString+"pageNo="+i+"\">"+i+"</a></span>\n");
}
}
//다음페이지 그룹
if(nextPage <= totalPageSize) {
result.append("\n <span class=\"btn_p next\"><a href=\"" + url + linkString + "pageNo=" + nextPage + "\" alt=\"다음\">></a></span>\n");
} else {
result.append("\n <span class=\"btn_p next\"><a href=\"" + url + linkString + "pageNo=" + endPage + "\" alt=\"다음\">></a></span>\n");
}
//마지막 페이지
if(page < totalPageSize) {
result.append("\n <span class=\"btn_p next_e\"><a href=\"" + url + linkString + "pageNo=" + totalPageSize + "\" alt=\"마지막페이지\">>></a></span>\n");
} else {
result.append("\n <span class=\"btn_p next_e\"><a href=\"#\" onclick=\"return false;\" alt=\"마지막페이지\">>></a></span>\n");
}
}
return result.toString();
}
/**
* 페이징처리를 하면서 필요한 파라미터값 세팅<p/>
*
* @param request HttpServletRequest
* @param startWith URI path
* @param ignoreParameter 제외할 파라미터 delimeter( | ) 로 구분
* @return parameter
* @throws Exception the exception
*/
public static String getParameter(HttpServletRequest request, String startWith, String ignoreParameter) throws Exception{
StringBuffer result = new StringBuffer();
String[] iParams = StringUtils.split(ignoreParameter, "|");
Enumeration names = request.getParameterNames();
while(names.hasMoreElements()) {
String name = (String) names.nextElement();
if(!ArrayUtils.contains(iParams, name)) {
String value = (String) request.getParameter(name);
if(StringUtils.isNotBlank(value)) {
if(StringUtils.isBlank(result.toString())) {
result.append(startWith);
result.append(name).append("=").append(value);
} else {
result.append("&").append(name).append("=").append(value);
}
}
}
}
return result.toString();
}
}
ResultDTO.java
package web.common.dto;
import java.io.Serializable;
import java.util.List;
import web.common.util.Paging;
public class ResultDTO<T> implements Serializable {
private static final long serialVersionUID = 7109492847903877377L;
private T obj;
private List<T> list;
private int count;
private Paging paging;
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Paging getPaging() {
return paging;
}
public void setPaging(Paging paging) {
this.paging = paging;
}
}
pageNaigation.jsp
<%@page import="web.common.util.PagingUtil"%>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String pageNum = StringUtils.defaultIfEmpty(request.getParameter("pageNo"), "1");
String pageSize = StringUtils.defaultIfEmpty(request.getParameter("pageSize"), "10");
String pagePerPageSize = StringUtils.defaultIfEmpty(request.getParameter("pagePerPageSize"), "10");
String totalCount = StringUtils.defaultIfEmpty(request.getParameter("totalPage"), "0");
String url = "";
String linkString = PagingUtil.getParameter(request, "", "pageNo|pageSize|pagePerPageSize|totalCount|totalPage|pageNumber");
/* System.err.println("linkString : "+linkString);
System.out.println("pageNum : "+pageNum);
System.out.println("pageSize : "+pageSize);
System.out.println("pagePerPageSize : "+pagePerPageSize);
System.out.println("totalCount : "+totalCount); */
String pagingHtml = PagingUtil.getPageNavigation(Integer.parseInt(pageNum), Integer.parseInt(totalCount), url, linkString, Integer.parseInt(pageSize), Integer.parseInt(pagePerPageSize));
//System.out.print(pagingHtml);
out.print(pagingHtml);
%>
Redirect.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html lang="ko">
<head>
</head>
<body>
<c:if test="${!empty redirect}" >
<form id="frm" name="frm" method="${redirect.method}" action="${ctx}${redirect.url}">
<c:forEach items="${redirect.params}" var="p">
<input type="hidden" name="${p.key}" value="${p.value}">
</c:forEach>
</form>
</c:if>
<script type="text/javascript">
<c:if test="${!empty redirect}" >
<c:if test="${!empty redirect.mag}">
alert('${redirect.mag}');
</c:if>
<c:if test="${redirect.openerReload}">
if(opener){
opener.document.location.reload();
}
</c:if>
<c:if test="${redirect.close}">
window.close();
</c:if>
<c:if test="${!redirect.close}">
document.frm.submit();
</c:if>
</c:if>
<c:if test="${empty redirect}" >
window.history.back();
</c:if>
</script>
</body>
</html>
list.jsp (페이징 부분)
<!-- 토탈 페이징 정보 -->
<div class="totalCot">
<span>전체</span>
<b class="txtBlack">${listVO.paging.totalCount}</b>
<span>건</span>
<b class="txtRed">${listVO.paging.curPage}</b>
<span>/${listVO.paging.totalLastPage} 페이지</span>
</div>
<!-- 페이징 -->
<c:if test="${!empty listVO.list}">
<div class="pagination">
<jsp:include page="/jsp.admin/pageNavigation.jsp">
<jsp:param name="pageNo" value="${listVO.paging.curPage}"/>
<jsp:param name="pageSize" value="10"/>
<jsp:param name="pagePerPageSize" value="10"/>
<jsp:param name="totalPage" value="${listVO.count}"/>
</jsp:include>
</div>
'Programming > Spring' 카테고리의 다른 글
[Spring] @ResponseBody와 jackson을 이용하여 JSON 사용하기 (0) | 2014.12.08 |
---|---|
[Spring] 어노테이션 Component-scan 분리하기 (0) | 2014.11.21 |
[Spring] 게시판 파일 업로드, 다운로드 (0) | 2014.11.18 |
[Spring] Interceptor 설정 (0) | 2014.11.18 |
[Spring] SpringMVC 간단한 파일 업로드 (0) | 2014.10.20 |