Criteria.java
package com.peta.domain;
public class Criteria {
private int page;
private int perPageNum;
private int groupnum;
public int getGroupnum() {
return groupnum;
}
public void setGroupnum(int groupnum) {
this.groupnum = groupnum;
}
public Criteria() {
this.page =1;
this.perPageNum =10;
}
public int getPage() {
return page;
}
public void setPage(int page) {
if(page <= 0) {
this.page = 1;
return;
}
this.page = page;
}
public int getPerPageNum() {
return this.perPageNum;
}
public void setPerPageNum(int perPageNum) {
if(perPageNum <=0 || perPageNum >100) {
this.perPageNum = 10;
}
this.perPageNum = perPageNum;
}
public int getPageStart() {
return (this.page - 1) * perPageNum;
}
@Override
public String toString() {
return "Criteria [page=" + page + ", perPageNum=" + perPageNum + "]";
}
}
SearchCriteria.java
package com.peta.domain;
public class SearchCriteria extends Criteria{
private String searchType;
private String keyword;
public String getSearchType() {
return searchType;
}
public void setSearchType(String searchType) {
this.searchType = searchType;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
@Override
public String toString() {
return "SearchCriteria [searchType=" + searchType + ", keyword=" + keyword + "]";
}
}
PageMaker.java
package com.peta.domain;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
public class PageMaker {
private int totalCount;
private int startPage;
private int endPage;
private boolean prev;
private boolean next;
//한화면에 보이는 페이지 갯수
private int displayPageNum =10;
private Criteria cri;
public void setCri(Criteria cri) {
this.cri =cri;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
calcData();
}
private void calcData() {
endPage = (int) (Math.ceil(cri.getPage() / (double) displayPageNum) * displayPageNum);
startPage = (endPage-displayPageNum) + 1;
int tempEndPage = (int) (Math.ceil(totalCount / (double) cri.getPerPageNum()));
if (endPage > tempEndPage) {
endPage = tempEndPage;
}
prev = startPage == 1? false:true;
next = endPage * cri.getPerPageNum() >= totalCount ? false :true;
}
public int getStartPage() {
return startPage;
}
public void setStartPage(int startPage) {
this.startPage = startPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
public boolean isPrev() {
return prev;
}
public void setPrev(boolean prev) {
this.prev = prev;
}
public boolean isNext() {
return next;
}
public void setNext(boolean next) {
this.next = next;
}
public int getDisplayPageNum() {
return displayPageNum;
}
public void setDisplayPageNum(int displayPageNum) {
this.displayPageNum = displayPageNum;
}
public int getTotalCount() {
return totalCount;
}
public Criteria getCri() {
return cri;
}
@Override
public String toString() {
return "PageMaker [totalCount=" + totalCount + ", startPage=" + startPage + ", endPage=" + endPage + ", prev="
+ prev + ", next=" + next + ", displayPageNum=" + displayPageNum + ", cri=" + cri + "]";
}
public String makeQuery(int page) {
UriComponents uriComponents =
UriComponentsBuilder.newInstance()
.queryParam("page", page)
.queryParam("perPageNum", cri.getPerPageNum())
.build();
return uriComponents.toUriString();
}
public String makeSearch(int page) {
UriComponents uriComponents =
UriComponentsBuilder.newInstance()
.queryParam("page", page)
.queryParam("perPageNum", cri.getPerPageNum())
.queryParam("groupnum", cri.getGroupnum())
.queryParam("searchType", ((SearchCriteria) cri).getSearchType())
.queryParam("keyword", encoding(((SearchCriteria) cri).getKeyword())).build();
return uriComponents.toUriString();
}
private String encoding(String keyword) {
if(keyword ==null || keyword.trim().length() ==0) {
return "";
}
try {
return URLEncoder.encode(keyword,"UTF-8");
}catch(UnsupportedEncodingException e) {
return "";
}
}
}
Mapper.xml
특이사항 : if 문의 입력을 and 로 시작시키기위해 favnum >0 을 미리 조건을 준다. CDATA 사용
<select id="listCriteria" resultType="com.peta.domain.UrlVO">
<![CDATA[
select favnum,url,memo,userid,groupnum,nowdate from fav_list where favnum > 0
]]>
<if test="groupnum != 0">
and groupnum = #{groupnum}
</if>
<if test="searchType != null">
<if test="searchType == 'u'.toString()">
and url like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'm'.toString()">
and memo like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'um'.toString()">
and (url like CONCAT('%', #{keyword}, '%') OR memo like CONCAT('%', #{keyword}, '%'))
</if>
</if>
order by favnum desc
limit #{pageStart},#{perPageNum}
</select>
<select id="countPaging" resultType="int">
<![CDATA[
select count(favnum) from fav_list where favnum > 0
]]>
<if test="groupnum != 0">
AND groupnum = #{groupnum}
</if>
<if test="searchType != null">
<if test="searchType == 'u'.toString()">
and url like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'm'.toString()">
and memo like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'um'.toString()">
and (url like CONCAT('%', #{keyword}, '%') OR memo like CONCAT('%', #{keyword}, '%'))
</if>
</if>
</select>
DAO
public List<CodeVO> listCriteria(Criteria cri) throws Exception;
public int countPaging(Criteria cri) throws Exception;
DAOImpl
@Override
public List<CodeVO> listCriteria(Criteria cri) throws Exception{
return session.selectList(namespace+".listCriteria",cri);
}
@Override
public int countPaging(Criteria cri) throws Exception{
return session.selectOne(namespace+".countPaging",cri);
}
Service
public List<CodeVO> listCriteria(Criteria cri) throws Exception;
public int countPaging(Criteria cri) throws Exception;
ServiceImpl
@Override
public List<CodeVO> listCriteria(Criteria cri) throws Exception{
return dao.listCriteria(cri);
}
@Override
public int countPaging(Criteria cri) throws Exception{
return dao.countPaging(cri);
}
jsp page
<div class="container">
<ul class="pagination justify-content-center">
<c:if test="${pageMaker.prev}">
<li class="page-item"><a class="page-link" href="list${pageMaker.makeSearch(pageMaker.startPage -1)}">Previous</a></li>
</c:if>
<c:forEach begin="${pageMaker.startPage}" end="${pageMaker.endPage}" var="idx">
<li class="page-item <c:out value="${pageMaker.cri.page == idx?'class = active':''}"/>" >
<a class="page-link" href="list${pageMaker.makeSearch(idx)}">${idx}</a>
</li>
</c:forEach>
<c:if test="${pageMaker.next && pageMaker.endPage > 0}">
<li><a class="page-link" href="list${pageMaker.makeSearch(pageMaker.endPage +1)}">Next</a></li>
</c:if>
</ul>
</div>
Controller
@RequestMapping(value="/code", method= {RequestMethod.GET,RequestMethod.POST})
public String codeGroupList(Model model,CodeVO vo,SearchCriteria cri) throws Exception {
logger.info("Code Group list start................");
String codeType = "code";
int groupnum = vo.getGroupnum();
model.addAttribute("nowgroupnum",groupnum);
model.addAttribute("codeGroup",service.codeGroupList());
model.addAttribute("codeType",codeType);
model.addAttribute("list",service.listCriteria(cri));
model.addAttribute("groupcount",service.groupCount(groupnum));
model.addAttribute("nowgroupname",service.groupName(groupnum));
PageMaker pageMaker = new PageMaker();
pageMaker.setCri(cri);
model.addAttribute("cri",cri);
pageMaker.setTotalCount(service.countPaging(cri));
model.addAttribute("pageMaker",pageMaker);
return "codeboard/codeList.page";
}