ssh自定义分页标签

来源:互联网 发布:大数据方向课题 编辑:程序博客网 时间:2024/05/19 19:44

java类



package com.chinasofti.articlesys.util;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.TagSupport;/** * 自定义的分页标签 * @author Liu * @version1.0 * @category 分页标签 创建时间:2012-8-17 17:00 */public class MyPage extends TagSupport {    private String url = null;    private int pageIndex;    private int pageMax;    public void setUrl(String url) {        this.url = url;    }    public String getUrl() {        return this.url;    }    public void setPageIndex(int pageIndex) {        this.pageIndex = pageIndex;    }    public int getPageIndex() {        return this.pageIndex;    }    public void setPageMax(int pageMax) {        this.pageMax = pageMax;    }    public int getPageMax() {        return this.pageMax;    }    @Override    public int doStartTag() throws JspException {        StringBuffer str = new StringBuffer();        if (pageIndex == 1) {            str.append("首页 上一页 ");        } else {            str.append(" <a href='" + url + "data.page=1'>首页</a> "                    + "<a href='" + url + "data.page=" + (pageIndex - 1) + "'>上一页</a> ");        }        if (pageIndex / 6 < 1.0 || pageMax < 10) {            for (int i = 1; i <= 9; i++) {                if (i <= pageMax) {                    if (pageIndex != i) {                        str.append("<a href='" + url + "data.page=" + i + "'>[" + i + "]</a> ");                    } else {                        str.append("  " + i + " ");                    }                }            }        } else if (pageIndex / 6 >= 1.0 && pageMax >= 10) {            int fri = 0;            int max = 0;            if (pageMax - pageIndex > 4) {                fri = pageIndex - 4;                max = pageIndex + 4;            } else {                fri = pageMax - 8;                max = pageMax;            }            for (int i = fri; i <= max; i++) {                if (i <= pageMax) {                    if (pageIndex != i) {                        str.append("<a href='" + url + "data.page=" + i + "'>[" + i + "]</a> ");                    } else {                        str.append("  " + i + " ");                    }                }            }        }        if (pageIndex == pageMax || pageMax < 2) {            str.append("下一页 尾页");        } else {            str.append("<a href='" + url + "data.page=" + (pageIndex + 1) + "'>下一页</a> "                    + "<a href='" + url + "data.page=" + pageMax + "'>尾页</a>");        }        str.append(" 跳转至:<select onchange=\"location='"+url+"data.page='+this.value;\">");        for(int i=1;i<=pageMax;i++){        str.append("<option value='"+i+"' "+(i==pageIndex?"selected":"")+">第"+i+"页</option>");        }        str.append("</select>");        try {            if (str.length()>0) {                pageContext.getOut().write(new String(str));            }        } catch (Exception e) {            throw new JspException(e);        }        return EVAL_PAGE;    }}

WEB-INF下.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>1.0</tlib-version>    <jsp-version>1.2</jsp-version>    <short-name>MyPage</short-name>    <!--OutputTag-->    <tag>        <name>page</name>        <tag-class>com.chinasofti.articlesys.util.MyPage</tag-class>        <body-content>empty</body-content>        <attribute>            <name>url</name>            <required>true</required>            <rtexprvalue>true</rtexprvalue>        </attribute>        <attribute>            <name>pageIndex</name>            <required>true</required>            <rtexprvalue>true</rtexprvalue>        </attribute>        <attribute>            <name>pageMax</name>            <required>true</required>            <rtexprvalue>true</rtexprvalue>        </attribute>    </tag></taglib>


java类


/** *  */package com.chinasofti.articlesys.util;import java.util.ArrayList;import java.util.List;/** * @author Liu *@createTime 2017-3-16 上午10:19:53 *@category 数据分页实体对象 */public class DataPage<T> {private List<T> list=new ArrayList<T>();//存放集合数据private int page=1;//当前第几页private int size=10;//每页显示数据个数private int totalPage=1;//总页数private int count=0;//总的记录个数public DataPage(int page,int size){this.page=page;this.size=size;}public DataPage(List<T> list,int page,int size,int count){this.list=list;this.page=page;this.size=size;this.count=count;this.totalPage=count%size==0?count/size:count/size+1;}public DataPage(List<T> list,String page,int size,int count){this.list=list;if(page!=null){this.page=Integer.valueOf(page);}this.size=size;this.count=count;this.totalPage=count%size==0?count/size:count/size+1;}public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}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 int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getCount() {return count;}public void setCount(int count) {this.count = count;this.totalPage=count%size==0?count/size:count/size+1;}}



action中

protected DataPage<T> data=new DataPage<T>(1,10);public DataPage<T> getData() {return data;}public void setData(DataPage<T> data) {this.data = data;}public String list() {data.setList(service.queryByPage(data.getPage(), data.getSize()));data.setCount(service.count());return "list";}

jsp页面中

<pt:page pageIndex="${data.page }" url="save!list?"  pageMax="${data.totalPage }" />