java 分页 标签

<tangs:pages cpage="%{cpage}" total="%{total}"
            url="lineSetList.action"
            keyValue="lineSetEntity.lineName=${lineSetEntity.lineName}&lineSetEntity.otherIP=${lineSetEntity.otherIP}" />
<?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.2.3</tlib-version>  
       <jsp-version>1.2</jsp-version>  
        <short-name>tangs</short-name>  
        <uri>/tangs</uri>  
        <display-name>Tangs Tags</display-name>  
          
           <tag>  
              <name>pages</name>  
              <tag-class>com.oms.omsWeb.web.util.PageTag</tag-class>  
              <body-content>empty</body-content>  
              <attribute>  
                  <name>cpage</name>  
                  <required>true</required>  
                 <rtexprvalue>true</rtexprvalue>  
             </attribute>  
             <attribute>  
                  <name>total</name>  
                  <required>true</required>  
                <rtexprvalue>true</rtexprvalue>  
              </attribute>  
            <attribute>  
                <name>url</name>  
                <required>true</required>  
                 <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <attribute>
                <name>styleClass</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <name>theme</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <name>keyValue</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
         </tag>  
</taglib>  

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oms.omsWeb.web.util;

import com.opensymphony.xwork2.util.ValueStack;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;
public class PageTag extends ComponentTagSupport {
    private String cpage;
    private String total;
    private String url;
    private String styleClass;
    private String theme;
    private String  keyValue;
    
    public void setKeyValue(String keyValue) {
        this.keyValue = keyValue;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }
    
    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public void setCpage(String cpage) {
        this.cpage = cpage;
    }

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

    public String getKeyValue() {
        return keyValue;
    }

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

    @Override
    public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) {
        return new Pages(arg0, arg1);
    }

    protected void populateParams() {
        super.populateParams();
        
        Pages pages = (Pages)component;
        pages.setCpage(cpage);
        pages.setTotal(total);
        pages.setUrl(url);
        pages.setStyleClass(styleClass);
        pages.setTheme(theme);
        pages.setKeyValue(keyValue);

    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oms.omsWeb.web.util;

import com.oms.omsWeb.common.util.I18NUtil;
import com.opensymphony.xwork2.util.ValueStack;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.components.Component;

/**
 * 分页逻辑Bean
 * @author tangs
 */
public class Pages extends Component {
    private HttpServletRequest request;
    private String cpage;
    private String total;
    private String url;
    private String styleClass;
    private String theme;
    private String  keyValue;
    public String getKeyValue() {
        return keyValue;
    }

    public void setKeyValue(String keyValue) {
        this.keyValue = keyValue;
    }


    
    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }
    
    
    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    
    public String getCpage() {
        return cpage;
    }

    public void setCpage(String cpage) {
        this.cpage = cpage;
    }

    public String getTotal() {
        return total;
    }

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

    
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    
    
    public Pages(ValueStack arg0, HttpServletRequest request) {
        super(arg0);
        this.request = request;
    }

    @Override
    public boolean start(Writer writer) {
        boolean result = super.start(writer);
        try {
            StringBuilder str = new StringBuilder();
            boolean isValid = true;
            
            //从ValueStack中取出数值
            if (isValid) {
                if (total.startsWith("%{") && total.endsWith("}")) {
                    total = total.substring(2, total.length() -1);
                    total = (String)this.getStack().findValue(total);
                    isValid = total == null ? false : true;
                } else {
                    isValid = false;
                }
            }
            if (isValid) {
                if (cpage.startsWith("%{") && cpage.endsWith("}")) {
                    cpage = cpage.substring(2, cpage.length() - 1);
                    cpage = (String)this.getStack().findValue(cpage);
                    isValid = cpage == null ? false : true;
                } else {
                    isValid = false;
                }
            }
            if (isValid) {
                if (url.startsWith("%{") && url.endsWith("}")) {
                    url = url.substring(2, url.length() - 1);
                    url = (String)this.getStack().findValue(url);
                    isValid = url == null ? false : true;
                }
            }
            if (isValid) {
                
                if(keyValue!=null &&!"".equals(keyValue))
                {
                     if (keyValue.startsWith("%{") && keyValue.endsWith("}")) {
                         keyValue = keyValue.substring(2, keyValue.length() - 1);
                         keyValue = String.valueOf(this.getStack().findValue(keyValue));
                         keyValue=keyValue.replace("&", "-");
                     }
                    
                }else{
                    keyValue="isId=1";
                }
               
            }
            
            if (isValid) {
                Integer cpageInt = Integer.valueOf(cpage);
                str.append("<div ");
                if (styleClass != null) {
                    str.append(" class='"+styleClass+"'>");
                } else {
                    str.append(">");
                }
                  String no=I18NUtil.getText("common.page.No");
                String pages1=I18NUtil.getText("common.page.Page");
                String total1=I18NUtil.getText("common.page.Total");
               String nextPage=I18NUtil.getText("common.page.NextPage");
               String PreviousPage=I18NUtil.getText("common.page.PreviousPage");
               String lastPage=I18NUtil.getText("common.page.LastPage");
               String firstPage=I18NUtil.getText("common.page.FirstPage");
                //文本样式
                if (theme == null || "text".equals(theme)) {
                    //当前页与总页数相等
                    if (cpage.equals(total)) {
                        //如果total = 1,则无需分页,显示“[第1页] [共1页]”
                        if ("1".equals(total)) {
                            str.append("["+no+" " + cpage + " "+pages1+"]");
                            str.append("&nbsp;["+total1+" " + total + " "+pages1+"]");
                        } else {
                            //到达最后一"+pages1+",显示“[首"+pages1+"] [上一"+pages1+"] [末"+pages1+"]”
                               str.append("["+no+" " + cpage + " "+pages1+"]");
                               str.append("&nbsp;["+total1+" " + total + " "+pages1+"]");
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=1&total="+total+"&url="+url+"&"+keyValue);
                            str.append("'>["+firstPage+"]</a> <a href='");
                            str.append(url);
                            str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url+"&"+keyValue);
                            str.append("'>["+PreviousPage+"]</a> <a href='");
                            str.append(url);
                            str.append("?cpage=" + total + "&total=" + total+"&url="+url+"&"+keyValue);
                            str.append("'>["+lastPage+"]</a>");
                        }
                    } else {
               
                        //当前"+pages1+"与总"+pages1+"数不相同
                        if ("1".equals(cpage)) {
                               str.append("["+no+" " + cpage + " "+pages1+"]");
                               str.append("&nbsp;["+total1+" " + total + " "+pages1+"]");
                            //"+no+"一"+pages1+",显示“["+firstPage+"] ["+nextPage+"] ["+lastPage+"]”
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=1&total="+total+"&url="+url+"&"+keyValue);
                            str.append("'>["+firstPage+"]</a> <a href='");
                            str.append(url);
                            str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url+"&"+keyValue);
                            str.append("'>["+nextPage+"]</a> <a href='");
                            str.append(url);
                            str.append("?cpage=" + total + "&total=" + total+"&url="+url+"&"+keyValue);
                            str.append("'>["+lastPage+"]</a>");
                        } else {
                            str.append("["+no+" " + cpage + " "+pages1+"]");
                            str.append("&nbsp;["+total1+" " + total + " "+pages1+"]");
                            //不是"+no+"一"+pages1+",显示“["+firstPage+"] ["+PreviousPage+"] ["+nextPage+"] ["+lastPage+"]”
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=1&total="+total+"&url="+url+"&"+keyValue);
                            str.append("'>["+firstPage+"]</a> <a href='");
                            str.append(url);
                            str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url+"&"+keyValue);
                            str.append("'>["+PreviousPage+"]</a> <a href='");
                            str.append(url);
                            str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url+"&"+keyValue);
                            str.append("'>["+nextPage+"]</a> <a href='");
                            str.append(url);
                            str.append("?cpage=" + total + "&total=" + total+"&url="+url+"&"+keyValue);
                            str.append("'>["+lastPage+"]</a>");
                        }
                    }
                } else if ("number".equals(theme)) {  //数字样式 [1 2 3 4 5 6 7 8 9 10 > >>]
                    Integer totalInt = Integer.valueOf(total);
                   
                    //如果只有一"+pages1+",则无需分"+pages1+"
                    str.append("[&nbsp;");
                    if (totalInt == 1) {
                        str.append("<font style=\"font-size: 30px;color: red\">1</font>&nbsp;");
                    } else {
                        //计算一"+total1+"分几组
                        int group = (totalInt - 1) / 10 + 1;
                        //当前"+no+"几组
                        int cgroup = (cpageInt - 1) / 10 + 1;
                        
                        if (cgroup > 1) {
                            //当前不是"+no+"一组,要显示“<< <”
                            //<<:返回前一组"+no+"一"+pages1+"
                            //<:返回前一"+pages1+"
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=" + ((cgroup - 2) * 10 + 1 ) + "&total=" + total+"&url="+url);
                            str.append("'>&laquo;</a>&nbsp;" );
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url);
                            str.append("'>&#8249;</a>&nbsp;" );
                        }
                        //10个为一组显示
                        for (int i = (cgroup - 1) * 10 + 1; i <= totalInt && i <= cgroup * 10; i++) {
                            if (cpageInt == i) { //当前"+pages1+"要加粗显示
                                str.append("<font style=\"font-size: 30px;color: red\"><strong>");  
                            }
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=" + i + "&total=" + total+"&url="+url);
                            str.append("'>" + i + "</a>&nbsp;");
                            if (cpageInt == i) {
                                str.append("</strong></font>");
                            }
                        }
                        //如果多于1组并且不是最后一组,显示“> >>”
                        if (group > 1&& cgroup != group) {
                            //>>:返回下一组最后一"+pages1+"
                            //>:返回"+nextPage+"
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url);
                            str.append("'>&#8250;</a>&nbsp;" );
                            str.append("<a href='");
                            str.append(url);
                            str.append("?cpage=" + ((cgroup * 10 + 10) > totalInt ? totalInt : (cgroup * 10 + 10)) + "&total=" + total+"&url="+url);
                            str.append("'>&raquo;</a>&nbsp;" );
                        }
                    }
                    str.append("]");
                }
                str.append("</div>");
            }
         
            writer.write(str.toString());
            
        } catch (IOException ex) {
            Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
}

import java.util.List;

public class PageModel {
    private int page = 1; // 当前页

    public int totalPages = 0; // 总页数

    private int pageRecorders;// 每页5条数据

    private int totalRows = 0; // 总数据数

    private int pageStartRow = 0;// 每页的起始数

    private int pageEndRow = 0; // 每页显示数据的终止数

    private boolean hasNextPage = false; // 是否有下一页

    private boolean hasPreviousPage = false; // 是否有前一页

    private List list;

    // private Iterator it;

    public PageModel(List list, int pageRecorders) {
        init(list, pageRecorders);// 通过对象集,记录总数划分
    }
    
    /** *//**
     * 初始化list,并告之该list每页的记录数
     * @param list
     * @param pageRecorders
     */
    public void init(List list, int pageRecorders) {
        this.pageRecorders = pageRecorders;
        this.list = list;
        totalRows = list.size();
        // it = list.iterator();
        hasPreviousPage = false;
        if ((totalRows % pageRecorders) == 0) {
            totalPages = totalRows / pageRecorders;
        } else {
            totalPages = totalRows / pageRecorders + 1;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }

        if (totalRows < pageRecorders) {
            this.pageStartRow = 0;
            this.pageEndRow = totalRows;
        } else {
            this.pageStartRow = 0;
            this.pageEndRow = pageRecorders;
        }
    }


    // 判断要不要分页
    public boolean isNext() {
        return list.size() > 5;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public String toString(int temp) {
        String str = Integer.toString(temp);
        return str;
    }

    public void description() {

        String description = "共有数据数:" + this.getTotalRows() +

        "共有页数: " + this.getTotalPages() +

        "当前页数为:" + this.getPage() +

        " 是否有前一页: " + this.isHasPreviousPage() +

        " 是否有下一页:" + this.isHasNextPage() +

        " 开始行数:" + this.getPageStartRow() +

        " 终止行数:" + this.getPageEndRow();

        System.out.println(description);
    }

    public List getNextPage() {
        page = page + 1;

        disposePage();

        System.out.println("用户凋用的是第" + page + "页");
        this.description();
        return getObjects(page);
    }

    /** *//**
     * 处理分页
     */
    private void disposePage() {

        if (page == 0) {
            page = 1;
        }

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
    }

    public List getPreviousPage() {

        page = page - 1;

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        this.description();
        return getObjects(page);
    }

    /** *//**
     * 获取第几页的内容
     *
     * @param page
     * @return
     */
    public List getObjects(int page) {
        if (page == 0)
            this.setPage(1);
        else
            this.setPage(page);
        this.disposePage();
        if (page * pageRecorders < totalRows) {// 判断是否为最后一页
            pageEndRow = page * pageRecorders;
            pageStartRow = pageEndRow - pageRecorders;
        } else {
            pageEndRow = totalRows;
            pageStartRow = pageRecorders * (totalPages - 1);
        }

        List objects = null;
        if (!list.isEmpty()) {
            objects = list.subList(pageStartRow, pageEndRow);
        }
        //this.description();
        return objects;
    }

    public List getFistPage() {
        if (this.isNext()) {
            return list.subList(0, pageRecorders);
        } else {
            return list;
        }
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }


    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }


    public List getList() {
        return list;
    }


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


    public int getPage() {
        return page;
    }


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


    public int getPageEndRow() {
        return pageEndRow;
    }


    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }


    public int getPageRecorders() {
        return pageRecorders;
    }


    public void setPageRecorders(int pageRecorders) {
        this.pageRecorders = pageRecorders;
    }


    public int getPageStartRow() {
        return pageStartRow;
    }


    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
    }


    public int getTotalPages() {
        return totalPages;
    }


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


    public int getTotalRows() {
        return totalRows;
    }


    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }


    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

}
public class PageBean {

    private int index;//当前页
    private int total;//总记录数
    private int pageSize;// 页大小
    public PageBean() {
        super();
        // TODO Auto-generated constructor stub
    }
    public int getIndex() {
        return index;
    }
    public void setIndex(int index) {
        this.index = index;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    /**
     * 取得总页数
     * @return
     */
    public int getTotalPage(){
        return (this.total+this.pageSize-1)/this.pageSize;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值