Created
July 21, 2019 08:01
-
-
Save antop-dev/f6926f23be387e03439a2c08f9de72c4 to your computer and use it in GitHub Desktop.
jstl paging custom tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.apache.taglibs.standard.tag.common.core.ParamParent; | |
| import org.apache.taglibs.standard.tag.common.core.ParamSupport; | |
| import org.apache.taglibs.standard.tag.common.core.UrlSupport; | |
| import javax.servlet.jsp.JspException; | |
| import javax.servlet.jsp.JspTagException; | |
| import javax.servlet.jsp.tagext.BodyTagSupport; | |
| import java.io.IOException; | |
| public class PagingTag extends BodyTagSupport implements ParamParent { | |
| private String url; | |
| private String context; | |
| private ParamSupport.ParamManager params; | |
| private int page; | |
| private int pageSize = 10; | |
| private int blockSize = 10; | |
| private int total; | |
| private String firstWord; | |
| private String previousWord; | |
| private String nextWord; | |
| private String lastWord; | |
| private boolean addedParameter = false; | |
| public PagingTag() { | |
| super(); | |
| init(); | |
| } | |
| private void init() { | |
| url = null; | |
| params = null; | |
| page = 1; | |
| pageSize = 10; | |
| blockSize = 10; | |
| total = 0; | |
| firstWord = "처음"; | |
| previousWord = "이전"; | |
| nextWord = "다음"; | |
| lastWord = "마지막"; | |
| } | |
| @Override | |
| public int doStartTag() throws JspException { | |
| params = new ParamSupport.ParamManager(); | |
| return EVAL_BODY_BUFFERED; | |
| } | |
| @Override | |
| public int doEndTag() throws JspException { | |
| if (total == 0 || pageSize == 0) { | |
| return EVAL_PAGE; | |
| } | |
| // System.out.println("page = " + page); | |
| // System.out.println("pageSize = " + pageSize); | |
| // System.out.println("blockSize = " + blockSize); | |
| // System.out.println("total = " + total); | |
| int startBlock = ((page - 1) / blockSize) * blockSize + 1; | |
| int endBlock = startBlock + blockSize - 1; | |
| int totalBlock = total / pageSize; | |
| if (total % pageSize > 0) { | |
| totalBlock++; | |
| } | |
| if (endBlock > totalBlock) { | |
| endBlock = totalBlock; | |
| } | |
| // System.out.println("startBlock = " + startBlock); | |
| // System.out.println("totalBlock = " + totalBlock); | |
| // System.out.println("endBlock = " + endBlock); | |
| String baseUrl = UrlSupport.resolveUrl(this.url, context, pageContext); | |
| baseUrl = params.aggregateParams(baseUrl); | |
| // System.out.println("baseUrl = " + baseUrl); | |
| StringBuffer sb = new StringBuffer(); | |
| sb.append("<div class=\"page_nav\">"); | |
| sb.append(" <ul class=\"clearfix\">"); | |
| if (page - 1 > 1) { | |
| sb.append("<li class=\"dis first\"><a href=\"" + makePagingUrl(baseUrl, 1) + "\">" + firstWord + "</a></li>"); | |
| } | |
| if (page > blockSize) { | |
| sb.append("<li class=\"dis prev\"><a href=\"" + makePagingUrl(baseUrl, startBlock - 1) + "\">" + previousWord + "</a></li>"); | |
| } | |
| for (int i = startBlock ; i <= endBlock; i++) { | |
| sb.append("<li "); | |
| if (i == page) { | |
| sb.append(" class=\"on\""); | |
| } | |
| sb.append(">"); | |
| sb.append("<a href=\""); | |
| sb.append(makePagingUrl(baseUrl, i)); | |
| sb.append("\">"); | |
| sb.append(i); | |
| sb.append("</a>"); | |
| sb.append("</li>"); | |
| } | |
| if (startBlock + blockSize < totalBlock) { | |
| sb.append("<li class=\"dis next\"><a href=\"" + makePagingUrl(baseUrl, startBlock + blockSize) + "\">" + nextWord + "</a></li>"); | |
| } | |
| if (page < totalBlock) { | |
| sb.append("<li class=\"dis last\"><a href=\"" + makePagingUrl(baseUrl, totalBlock) + "\">" + lastWord + "</a></li>"); | |
| } | |
| sb.append(" </ul>"); | |
| sb.append("</div>"); | |
| try { | |
| pageContext.getOut().print(sb.toString()); | |
| } catch (IOException e) { | |
| throw new JspTagException(e); | |
| } | |
| return EVAL_PAGE; | |
| } | |
| private String makePagingUrl(String baseUrl, int page) { | |
| return baseUrl + (addedParameter ? "&" : "?") + "page=" + page; | |
| } | |
| public void release() { | |
| init(); | |
| } | |
| @Override | |
| public void addParameter(String name, String value) { | |
| params.addParameter(name, value); | |
| addedParameter = true; | |
| } | |
| public void setUrl(String url) { | |
| this.url = url; | |
| } | |
| public void setContext(String context) { | |
| this.context = context; | |
| } | |
| public void setPage(int page) { | |
| this.page = page; | |
| } | |
| public void setPageSize(int pageSize) { | |
| this.pageSize = pageSize; | |
| } | |
| public void setBlockSize(int blockSize) { | |
| this.blockSize = blockSize; | |
| } | |
| public void setTotal(int total) { | |
| this.total = total; | |
| } | |
| public void setFirstWord(String firstWord) { | |
| this.firstWord = firstWord; | |
| } | |
| public void setPreviousWord(String previousWord) { | |
| this.previousWord = previousWord; | |
| } | |
| public void setNextWord(String nextWord) { | |
| this.nextWord = nextWord; | |
| } | |
| public void setLastWord(String lastWord) { | |
| this.lastWord = lastWord; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment