自定义分页标签实现

分页标签如下:
package com.sz.kcygl.web.tag;

import java.util.Enumeration;

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

import com.sz.kcygl.common.constants.KcyglConstants;
import com.sz.kcygl.common.util.Utils;


/**
 * 分页标签
 */
public class PageSplitTag extends TagSupport {

    private String url = ""; // 页面指向地址

    private String pageNo = ""; // 当前页面,字符串型,由外面传入

    private String paramsStr = ""; // 组装后的参数字符串

    private int totalPages = 1; // 总页面数

    private int count = 0; // 总记录数

    private int intPageNo = 1; // 当前页面
    
    private String type ;

    private int pageSize = KcyglConstants.PAGE_SIZE_DEFAULT; // 每一页面显示的最大记录数

    public PageSplitTag() {
    }

    public int doStartTag() throws JspException {
        if (url == null) {
            url = "";
        }
        url = url.trim();

        HttpServletRequest request = (HttpServletRequest) pageContext
                .getRequest();
        Enumeration en = request.getParameterNames();
        StringBuffer param = new StringBuffer();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            if ("pageNo".equals(key) || key.toLowerCase().startsWith("submit")||"pageSize".equals(key))
                continue;
            String value = Utils.trim(request.getParameter(key));
            if (value.equals(""))
                continue;
            param.append("&" + key + "=" + Utils.encodeStr(value));
        }
        paramsStr = param.toString();

        try {
            intPageNo = Utils.parseInt(pageNo, 1);
            if(intPageNo<1){
                intPageNo = 1;
            }
        } catch (Exception e) {
        }
        if (count % pageSize > 0) {
            totalPages = count / pageSize + 1;
        } else {
            totalPages = count / pageSize;
        }
        if (intPageNo > totalPages) {
            intPageNo = totalPages;
        }
        return (SKIP_BODY);
    }

    public int doEndTag() throws JspException {
        StringBuffer reStr = new StringBuffer();
        reStr.append("<div class='scrollpage'>");
        reStr.append("<form name='splitPageForm' id='splitPageForm' method='post' ");
        reStr.append("action='" + url + addParams(paramsStr) + "'>");
        
        reStr.append("<ul class='page_btn'>");
        
        if (totalPages < 2) {
            reStr.append("<li class='ini_un'><a href='javascript:void(0)'></a></li>");
            reStr.append("<li class='pre_un'><a href='javascript:void(0)'></a></li>");
            reStr.append("<li>"+intPageNo+"/"+totalPages+"</li>");
            reStr.append("<li class='next_un'><a href='javascript:void(0)'></a></li>");
            reStr.append("<li class='end_un'><a href='javascript:void(0)'></a></li>");
        } else {
            if (intPageNo < 2) {
                reStr.append("<li class='ini_un'><a href='javascript:void(0)'></a></li>");
                reStr.append("<li class='pre_un'><a href='javascript:void(0)'></a></li>");
                reStr.append("<li>"+intPageNo+"/"+totalPages+"</li>");
                reStr.append(getUrl(intPageNo + 1, "next"));
                reStr.append(getUrl(totalPages, "end"));
            } else if (intPageNo == totalPages) {
                reStr.append(getUrl(1, "ini"));
                reStr.append(getUrl(intPageNo - 1, "pre"));
                reStr.append("<li>"+intPageNo+"/"+totalPages+"</li>");
                reStr.append("<li class='next_un'><a href='javascript:void(0)'></a></li>");
                reStr.append("<li class='end_un'><a href='javascript:void(0)'></a></li>");
            } else {
                reStr.append(getUrl(1, "ini"));
                reStr.append(getUrl(intPageNo - 1, "pre"));
                reStr.append("<li>"+intPageNo+"/"+totalPages+"</li>");
                reStr.append(getUrl(intPageNo + 1, "next"));
                reStr.append(getUrl(totalPages, "end"));
            }
        }
        reStr.append("</ul>");
        
        reStr.append("<ul class='page_num'>");
        reStr.append("<li><input class='bottom_btn' name='' type='button' value=' Go ' onclick=\"addCookie('pageSize',document.getElementById('pageSize').value,720); document.getElementById('splitPageForm').submit();\" /></li>");
        reStr.append("<li> <span>跳转至</span><input style='vertical-align:center;' size='2' name='pageNo' type='text' /><span>页</span>  <span>每页</span><input size='2' style='vertical-align:center;' name='pageSize' id='pageSize' type='text' value='"+pageSize+"' /><span>条</span></li>");
        reStr.append("<li>共"+count+"条匹配记录</li>");
        reStr.append("</ul");
    
        reStr.append("</form></div>");
        JspWriter writer = pageContext.getOut();
        try {
            writer.println(new String(reStr.toString().getBytes()));
        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }
        return (EVAL_PAGE);
    }

    private String getUrl(int pageNo, String name) {
        return "<li class='"+name+"'><a href='" + dealUrl(url, pageNo)
                + "'></a></li>";
    }

    private String dealUrl(String url, int pageNo) {
        return url + "?pageNo=" + pageNo +"&pageSize="+pageSize+ paramsStr;
    }

    private String addParams(String params) {
        if (params == null || params.equals("")) {
            return "";
        }
        return "?" + params.substring(1);
    }

    public void release() {
        super.release();
    }

    public void setPageNo(String pageNo) {
        this.pageNo = pageNo;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

}
分页用到的两个实体对象:
package com.sz.kcygl.common.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class PageList<T> implements Serializable {
    
    public PageList() {

    }

    public PageList(PageProperty pp, int allCount, List<T> list) {    
        if(pp.getNpage()>0){
            this.page = pp.getNpage();
        }
        if(pp.getNpagesize()>0){
            this.pageSize = pp.getNpagesize();
        }
        this.totalRecords = allCount;
        if (totalRecords % pageSize > 0) {
            this.totalPages = totalRecords / pageSize + 1;
        } else {
            this.totalPages = totalRecords / pageSize;
        }
        this.setRecords(list);
    }

    private int page = 1;

    private int totalRecords;

    private int totalPages;

    private int pageSize = 20;

    private int numbersPerBlock = 10;

    private List<T> records = new ArrayList<T>();

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public int getPage() {
        return page;
    }

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

    public int getPageNumber() {
        int pageNumber = 0;
        if (totalRecords % pageSize == 0)
            pageNumber = totalRecords / pageSize;
        else
            pageNumber = 1 + totalRecords / pageSize;

        return pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    /**
     * first row count of current page, start from 1
     *
     * @return
     */
    public int getFirstRow() {
        return (page - 1) * pageSize + 1;
    }

    /**
     * last row count of current page
     *
     * @return
     */
    public int getLastRow() {
        return page == getPageNumber() ? getTotalRecords() : page * pageSize;
    }

    public int getPreviousPage() {
        return page > 1 ? page - 1 : page;
    }

    public int getNextPage() {
        return page < getPageNumber() ? page + 1 : page;
    }

    public int getBlocks() {
        if (this.getPageNumber() % this.numbersPerBlock == 0) {
            return this.getPageNumber() / this.numbersPerBlock;
        } else {
            return 1 + this.getPageNumber() / this.numbersPerBlock;
        }
    }

    public int getBlock() {
        if (this.getPage() % this.numbersPerBlock == 0) {
            return this.getPage() / this.numbersPerBlock;
        } else {
            return 1 + this.getPage() / this.numbersPerBlock;
        }
    }

    public int getNumbersPerBlock() {
        return numbersPerBlock;
    }

    public void setNumbersPerBlock(int numberPerBlock) {
        this.numbersPerBlock = numberPerBlock;
    }

    public int getPageOfPrevBlock() {
        if (this.getBlock() > 1) {
            return (this.getBlock() - 1) * this.getNumbersPerBlock();
        } else {
            return 1;
        }
    }

    public int getPageOfNextBlock() {
        if (this.getBlock() < this.getBlocks()) {
            return this.getBlock() * this.getNumbersPerBlock() + 1;
        } else {
            return this.getTotalRecords();
        }
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }
}


package com.sz.kcygl.common.page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PageProperty {
    
    private int npage;//页码
    private int nfirstindex;//查询起点
    private int npagesize;//查询数量,0表示全部
    private String searchString;//查询条件 where field=?
    private String orderString;//排序条件 order by id desc
    private List parameterList;// 查询条件参数列表
    private HashMap paramMap; //参数map

    public PageProperty(int page, int pagesize,String searchString, String orderString){
        this.npagesize = pagesize;
        this.npage = page;
        this.searchString = searchString;
        this.orderString = orderString;
    }
    public PageProperty(){
            this.npage=1;
        this.npagesize = 10;
        this.nfirstindex = 0;
        this.searchString = "";
        this.orderString = "";
    }

    public int getNfirstindex() {
            nfirstindex=(npage-1)*npagesize;
        return nfirstindex;
    }

    public void setNfirstindex(int nfirstindex) {
        this.nfirstindex = nfirstindex;
    }

    public int getNpagesize() {
        return npagesize;
    }

    public void setNpagesize(int npagesize) {
        this.npagesize = npagesize;
    }

    public String getOrderString() {
        return orderString;
    }

    public void setOrderString(String orderString) {
        this.orderString = orderString;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }
    public List getParameterList() {
        return parameterList;
    }
    public void setParameterList(List parameterList) {
        this.parameterList = parameterList;
    }
    public void addParamter(Object o){//增加参数
        initParameterList();
        parameterList.add(o);
    }
    public void addParamter(int index,Object o){ //增加参数
        initParameterList();
        parameterList.add(index,o);
    }
    public void clearParamter(){ //增加参数
        initParameterList();
        parameterList.clear();
    }
    public void initParameterList(){//初始化参数列表
        if(parameterList==null){
            parameterList=new ArrayList();
        }
    }
    public int getNpage() {
        return npage;
    }
    public void setNpage(int page) {
        npage = page;
    }


    public HashMap getParamMap() {
        initParamMap();
        return paramMap;
    }

    public void putParamMap(String name,Object o){ //增加参数
        initParamMap();
        paramMap.put(name,o);
    }
    public void clearParamMap(){ //增加参数
        initParamMap();
        paramMap.clear();
    }
    public void initParamMap(){//初始化参数列表
        if(paramMap==null){
            paramMap=new HashMap();
        }
    }

    public int getPageStart(){
        return (npage - 1)*npagesize;
    }
    
    public int getPageEnd(){
        return npage*npagesize;
    }
    
}
tld文件:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    
    <description>kcygl tags</description>
    <tlib-version>1.0</tlib-version>
    <short-name>kcygl</short-name>
    
    <tag>
        <name>pageSplit</name>
        <tag-class>com.sz.kcygl.web.tag.PageSplitTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>pageNo</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>count</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>url</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>params</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>type</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>pageSize</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>columnSort</name>
        <tag-class>com.sz.kcygl.web.tag.ColumnSortTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>url</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>column</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>columnShowName</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>sortType</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>sortColumn</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

你可能感兴趣的:(自定义分页标签实现)