008-PageBean类模板

1 PageBean模板一

package ${enclosing_package};

import java.util.ArrayList;
import java.util.List;
public class ${primary_type_name}<T> {
    
    //当前页
    private int currentPage;
    //当前页显示的条数
    private int currentCount;
    //总条数
    private int totalCount;
    //总页数
    private int totalPage;
    //每页显示的数据
    private List<T> productList = new ArrayList<T>();
    
    
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getCurrentCount() {
        return currentCount;
    }
    public void setCurrentCount(int currentCount) {
        this.currentCount = currentCount;
    }
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public List<T> getProductList() {
        return productList;
    }
    public void setProductList(List<T> productList) {
        this.productList = productList;
    }
}

 

2 PageBean模板二

package ${enclosing_package};

import java.util.List;

public class ${primary_type_name} {

    //当前页
    private Integer currentPage;
    //当前页显示的条数
    private Integer pageSize;
    //总页数
    private Integer totalPage;
    //总记录数
    private Integer totalCount;
    
    //分页列表的数据
    private List list ;

    public ${primary_type_name}(Integer currentPage, Integer pageSize, Integer totalCount) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        
        //如果没有指定显示那一页,就显示第一页。
        if(this.currentPage==null){
            this.currentPage=1;
        }
        
        //如果没有指定每页显示几条数据,就默认显示3条。
        if(this.pageSize==null){
            this.pageSize=3;
        }
        
        //计算总页数
        this.totalPage = (this.totalCount+this.pageSize-1)/this.pageSize;
        //this.totalPage = (int) Math.ceil((this.totalCount*1.0)/this.pageSize);
        
        //判断当前页数是否超出范围
        //不能小于1
        if(this.currentPage<1){
            this.currentPage=1;
        }
        //不能大于总页数
        if(this.currentPage>this.totalPage){
            this.currentPage=this.totalPage;
        }
    }

    
    //计算起始索引
    public Integer getStart(){
        return (this.currentPage-1)*this.pageSize;
    }
    
    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

}

3 PageBean模板三(easyui)

package com.test.bos.utils;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
/**
 * 封装分页属性
 * @author jepson
 *
 */
public class PageBean {

    private Integer currentPage;//当前页
    
    private Integer pageSize; //当前页显示的条数
    
    private DetachedCriteria detachedCriteria; //查询条件
    
    private Integer total; //总记录数  ,使用easyui的datagrid的分页这是固定的只能是total
    
    private List rows; //当前页需要展示的数据集合,使用easyui的datagrid的分页这是固定的只能是rows

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

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

    public DetachedCriteria getDetachedCriteria() {
        return detachedCriteria;
    }

    public void setDetachedCriteria(DetachedCriteria detachedCriteria) {
        this.detachedCriteria = detachedCriteria;
    }

    public Integer getTotal() {
        return total;
    }

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

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }
}

1 针对模板三-通用的分页查询方法

//通用的分页查询方法
@Override
public void pageQuery(PageBean pageBean) {
    Integer currentPage = pageBean.getCurrentPage();
    Integer pageSize = pageBean.getPageSize();
    DetachedCriteria detachedCriteria = pageBean.getDetachedCriteria();
    
    //查询total---总数据量
    detachedCriteria.setProjection(Projections.rowCount());
    List<Long> countList = (List<Long>) super.getHibernateTemplate().findByCriteria(detachedCriteria);
    Long count = 0L;
    if(countList!=null&&countList.size()>0){
        count = countList.get(0);
    }
    pageBean.setTotal(count.intValue());

    //分页列表的数据
    detachedCriteria.setProjection(null); //清空上面设置的聚合查询
    int firstResult = (currentPage-1)*pageSize; //分页查询起始页
    int maxResults = pageSize;    //分页查询每页显示的条数
    List list = super.getHibernateTemplate().findByCriteria(detachedCriteria, firstResult, maxResults);
    pageBean.setRows(list);
}

 

4 模板四 -自定义标签实现分页

需要注意:提交方式是get提交,需要解决中文乱码问题

1 commons.tld

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>2.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>common</short-name>
    <uri>http://jepson.com/common/</uri>
    <display-name>Common Tag</display-name>
    <description>Common Tag library</description>

    <tag>
        <name>page</name>
        <tag-class>com.test.common.utils.NavigationTag</tag-class>
        <body-content>JSP</body-content>
        <description>create navigation for paging</description>
        <attribute>
            <name>bean</name>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>number</name>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>url</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

2.NavigationTag

package com.test.common.utils;

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;

/**
 * 显示格式 上一页 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 javax.servlet.jsp.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

package com.test.common.utils;

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;
    }
}

 

posted @ 2018-01-25 14:02  Jepson6669  阅读(285)  评论(0编辑  收藏  举报