1. 程式人生 > >JSP頁面分頁標籤的使用

JSP頁面分頁標籤的使用

1.組成
1. 兩個utils工具類
2. 一個tld檔案
2.NavigationTag.java
package oranges.util;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import
org.apache.taglibs.standard.tag.common.core.UrlSupport; /** * 顯示格式 上一頁 1 2 3 4 5 下一頁 */ public class NavigationTag extends TagSupport { static final long serialVersionUID = 2372405317744358833L; /** * request 中用於儲存Page<E> 物件的變數名,預設為“page” */ private String bean = "page"; /** * 分頁跳轉的url地址,此屬性必須 */
private String url = null; /** * 顯示頁碼數量 */ private int number = 5; @Override public int doStartTag() throws JspException { JspWriter writer = pageContext.getOut(); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); Page page = (Page)request.getAttribute(bean); if
(page == null) return SKIP_BODY; url = resolveUrl(url, pageContext); try { //計算總頁數 int pageCount = page.getTotal() / page.getSize(); if (page.getTotal() % page.getSize() > 0) { pageCount++; } writer.print("<nav><ul class=\"pagination\">"); //顯示“上一頁”按鈕 if (page.getPage() > 1) { String preUrl = append(url, "page", page.getPage() - 1); preUrl = append(preUrl, "rows", page.getSize()); writer.print("<li><a href=\"" + preUrl + "\">上一頁</a></li>"); } else { writer.print("<li class=\"disabled\"><a href=\"#\">上一頁</a></li>"); } //顯示當前頁碼的前2頁碼和後兩頁碼 //若1 則 1 2 3 4 5, 若2 則 1 2 3 4 5, 若3 則1 2 3 4 5, //若4 則 2 3 4 5 6 ,若10 則 8 9 10 11 12 int indexPage = (page.getPage() - 2 > 0)? page.getPage() - 2 : 1; for(int i=1; i <= number && indexPage <= pageCount; indexPage++, i++) { if(indexPage == page.getPage()) { writer.print( "<li class=\"active\"><a href=\"#\">"+indexPage+"<span class=\"sr-only\">(current)</span></a></li>"); continue; } String pageUrl = append(url, "page", indexPage); pageUrl = append(pageUrl, "rows", page.getSize()); writer.print("<li><a href=\"" + pageUrl + "\">"+ indexPage +"</a></li>"); } //顯示“下一頁”按鈕 if (page.getPage() < pageCount) { String nextUrl = append(url, "page", page.getPage() + 1); nextUrl = append(nextUrl, "rows", page.getSize()); writer.print("<li><a href=\"" + nextUrl + "\">下一頁</a></li>"); } else { writer.print("<li class=\"disabled\"><a href=\"#\">下一頁</a></li>"); } writer.print("</nav>"); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } private String append(String url, String key, int value) { return append(url, key, String.valueOf(value)); } /** * 為url 參加引數對兒 * * @param url * @param key * @param value * @return */ private String append(String url, String key, String value) { if (url == null || url.trim().length() == 0) { return ""; } if (url.indexOf("?") == -1) { url = url + "?" + key + "=" + value; } else { if(url.endsWith("?")) { url = url + key + "=" + value; } else { url = url + "&amp;" + key + "=" + value; } } return url; } /** * 為url 新增翻頁請求引數 * * @param url * @param pageContext * @return * @throws JspException */ private String resolveUrl(String url, javax.servlet.jsp.PageContext pageContext) throws JspException{ //UrlSupport.resolveUrl(url, context, pageContext) Map params = pageContext.getRequest().getParameterMap(); for (Object key:params.keySet()) { if ("page".equals(key) || "rows".equals(key)) continue; Object value = params.get(key); if (value == null) continue; if (value.getClass().isArray()) { url = append(url, key.toString(), ((String[])value)[0]); } else if (value instanceof String) { url = append(url, key.toString(), value.toString()); } } return url; } /** * @return the bean */ public String getBean() { return bean; } /** * @param bean the bean to set */ public void setBean(String bean) { this.bean = bean; } /** * @return the url */ public String getUrl() { return url; } /** * @param url the url to set */ public void setUrl(String url) { this.url = url; } public void setNumber(int number) { this.number = number; } }
3.page.java
package oranges.util;

import java.util.List;

public class Page<T> {

    private int total;      //總的記錄數
    private int page;       //當前頁數
    private int size;       //每頁的記錄數
    private List<T> rows;   //每頁資料的集合

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}
4.頁面資料
<%@ taglib prefix="oranges" uri="http://oranges.cn/common/"%>
<div class="col-md-12 text-right">
   <oranges:page url="${pageContext.request.contextPath }/customer/list.action" />
</div>