自定义分页标签

自定义分页标签http://www.diybl.com/ 2007-12-3  网络 点击:9  [ 评论 ]
文章搜索:  



下面分别是二个javabean作为标签和数据间的转换.



package com.lanzhengwu.web.util;
public class Pager...{

    private long totalRows = 0; // 记录总数:需传参数

    private int totalPages = 0; // 总页数

    private int pageSize =UIConstants.PAGE_SIZE; // 每页显示数据条数,默认为10条记录

    private int currentPage = 1; // 当前页数

    private boolean hasPrevious = false; // 是否有上一页

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

    public Pager() ...{

    }



    public Pager(long totalRows,int pagesize) ...{

        this.totalRows = totalRows;
       
        long temp=totalRows % pagesize == 0 ? totalRows / pagesize
                : totalRows / pagesize + 1;
        totalPages=Integer.parseInt(temp+"");
        refresh(); // 刷新当前页面信息

    }

    public void setCurrentPage(int currentPage) ...{

        this.currentPage = currentPage;

        refresh();

    }


    public void setPageSize(int pageSize) ...{

        this.pageSize = pageSize;

        refresh();

    }



    public void setTotalPages(int totalPages) ...{

        this.totalPages = totalPages;

        refresh();

    }

    public void setTotalRows(long totalRows) ...{

        this.totalRows = totalRows;

        refresh();

    }


       // 跳到第一页

    public void first() ...{

        currentPage = 1;

        this.setHasPrevious(false);

        refresh();

    }


       // 取得上一页(重新设定当前页面即可)

    public void previous() ...{
        refresh();

    }


       // 取得下一页

    public void next() ...{

        refresh();

    }


       // 跳到最后一页

    public void last() ...{

        currentPage = totalPages;

        this.setHasNext(false);

        refresh();

    }


    public boolean isHasNext() ...{

        return hasNext;

    }


    /** *//**

     * @param hasNext The hasNext to set.

     */

    public void setHasNext(boolean hasNext) ...{

        this.hasNext = hasNext;

    }


    public boolean isHasPrevious() ...{

        return hasPrevious;

    }


    /** *//**

     * @param hasPrevious The hasPrevious to set.

     */

    public void setHasPrevious(boolean hasPrevious) ...{

        this.hasPrevious = hasPrevious;

    }


       // 刷新当前页面信息

    public void refresh() ...{

        if (totalPages <= 1) ...{

            hasPrevious = false;

            hasNext = false;

        } else if (currentPage <= 1) ...{

            hasPrevious = false;

            hasNext = true;

        } else if (currentPage >= totalPages) ...{

            hasPrevious = true;

            hasNext = false;

        } else ...{

            hasPrevious = true;

            hasNext = true;

        }

    }


    public int getCurrentPage() ...{
        return currentPage;
    }



    public int getPageSize() ...{
        return pageSize;
    }


    public int getTotalPages() ...{
        return totalPages;
    }


    public long getTotalRows() ...{
        return totalRows;
    }

}









package com.lanzhengwu.web.util;
import javax.servlet.http.*;

public class PagerHelp ...{
    public static Pager getPager(HttpServletRequest request,long totalRows,int pagesize)...{
        Pager pager=new Pager(totalRows, pagesize);
       
        String currentPage=request.getParameter("pageno");//这里还是接收的页面传过来的Page值
        if(currentPage==null || currentPage=="")currentPage="1";
        pager.setCurrentPage(Integer.parseInt(currentPage));
        pager.setPageSize(pagesize);
        String method=request.getParameter("method1");
       
        if("first".equals(method))...{
            pager.first();
        }else if("previous".equals(method))...{
            pager.previous();
        }else if("next".equals(method))...{
            pager.next();
        }else if("last".equals(method))...{
            pager.last();
        }else...{
            pager.refresh();//无论如何一定要刷新一下页面信息,否则在页面中得不到totalpage
        }
        return pager;
       
    }
}

标签类:





package com.lanzhengwu.web.util;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

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

public class PageTag  extends TagSupport...{

    /** *//**
     *
     */
    private static final long serialVersionUID = 865166066848853713L;

    private long totalRows=0;//总记录数
   
    private int pagesize=UIConstants.PAGE_SIZE;//这不是必须的参数,默认为:UIConstants.PAGE_SIZE
   
     private String forward="";//转向页面:需传参数
    
     private Map<String,Object> paras=null;//页面的参数及值(因为用多个值的话要用MAP来装)
    
     private String para="";//页面的参数(单个时)
    
     private String value="";//参数的值

     private String act="";//选择分页模式
    
     private String isHasPara="no";//是否有多个参数,默认为没有


    public int doStartTag()...{
        JspWriter out=pageContext.getOut();
        if("yes".equalsIgnoreCase(getIsHasPara()))...{//多参模式
            this.setForward(forward, paras);
        }else...{//单参模式
            this.setForward1(forward, para, value);
        }
        HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
        Pager pager=com.cn001cp.web.util.PagerHelp.getPager(request, totalRows,pagesize);
        int npage=pager.getCurrentPage();//当前页码
        int totalpage=pager.getTotalPages();//当前总页数
       

            try ...{
           
                if(totalpage>1)...{
                    out.println("<center>");
                   
                if(pager.isHasPrevious() && !pager.isHasNext())...{

                    out.println("<a href=''"+forward+"pageno=1&method1=first''>首页</a>");
                    out.println("<a href=''"+forward+"pageno="+(npage-1)+"&method1=previous''>上一页</a>");
                    out.println("<a href=''"+forward+"pageno="+(npage+1)+"&method1=next'' disabled=false onclick="return dislink()">下一页</a>");
                    out.println("<a href=''"+forward+"pageno="+totalpage+"&method1=last'' disabled=false onclick="return dislink()">尾页</a>");
                   
                }else if(pager.isHasNext() && !pager.isHasPrevious())...{
                    out.println("<a href=''"+forward+"pageno=1&method1=first'' disabled=false onclick="return dislink()">首页</a>");
                    out.println("<a href=''"+forward+"pageno="+(npage-1)+"&method1=previous'' disabled=false onclick="return dislink()">上一页</a>");
                    out.println("<a href=''"+forward+"pageno="+(npage+1)+"&method1=next''>下一页</a>");
                    out.println("<a href=''"+forward+"pageno="+totalpage+"&method1=last''>尾页</a>");   
                }else if(pager.isHasNext() && pager.isHasPrevious()) ...{
                    out.println("<a href=''"+forward+"pageno=1&method1=first''>首页</a>");
                    out.println("<a href=''"+forward+"pageno="+(npage-1)+"&method1=previous''>上一页</a>");
                    out.println("<a href=''"+forward+"pageno="+(npage+1)+"&method1=next''>下一页</a>");
                    out.println("<a href=''"+forward+"pageno="+totalpage+"&method1=last''>尾页</a>");
                }
                    out.println("<script>function change1(page){");
                   
                    out.println("window.location=''"+forward+"pageno=''+page.value");
                    out.println("}");
                    out.println("function dislink(){");
                    out.println("return false;");
                    out.println("}</script>");
                    out.println("<select name="select1" onchange="change1(this)">");
                    for(int i=0;i<totalpage;i++)...{
                        if(npage!=i+1)...{//不显示当前页数字
                            out.print("<option value="+(i+1)+">"+(i+1)+"</option>");
                        }
                    }
                    out.println("</select>");
                   
                    out.println(""+npage+"/"+totalpage+"");
                    out.println("共"+totalRows+"条记录");
                    out.println("</center>");
            }
            } catch (IOException e) ...{
                // TODO 自动生成 catch 块
                e.printStackTrace();
            }
           
        return TagSupport.SKIP_BODY;
    }

    public void setForward(String forward,Map<String,Object> paras) ...{//设置url(为多参模式时)
       
        this.paras=paras;
        Set<Map.Entry<String, Object>> set = paras.entrySet();
        if(forward.indexOf("?")==-1)...{//不存在问号
            forward=forward+"?pagesize="+getPagesize()+"&";
            if(!paras.isEmpty())...{            
                for(Map.Entry<String,Object> hash :set)...{
                forward+=hash.getKey()+"="+hash.getValue()+"&";
                }
            }
        }else...{
            forward=forward+"&pagesize="+getPagesize()+"&";
            if(!paras.isEmpty())...{
                for(Map.Entry<String,Object> hash :set)...{
                forward+=hash.getKey()+"="+hash.getValue()+"&";
                }
            }   
        }
       
        this.forward = forward;
    }
    public void setForward1(String forward,String para,String value)...{//设置url(为单参模式时)
        if(forward.indexOf("?")==-1)...{//不存在问号
            forward=forward+"?pagesize="+getPagesize()+"&";
            if(para!="" && value!="")...{
                forward+=para+"="+value+"&";
            }
        }else...{
            forward=forward+"&pagesize="+getPagesize()+"&";
            if(para!="" && value!="")...{
                forward+=para+"="+value+"&";
            }
        }
    }


    public int getPagesize() ...{
        return pagesize;
    }

    public void setPagesize(int pagesize) ...{
        this.pagesize = pagesize;
    }

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

    public void setForward(String forward) ...{
        this.forward = forward;
    }



    public String getAct() ...{
        return act;
    }



    public String getForward() ...{
        return forward;
    }


    public long getTotalRows() ...{
        return totalRows;
    }


    
    public String getIsHasPara() ...{
        return isHasPara;
    }



    public void setIsHasPara(String isHasPara) ...{
        this.isHasPara = isHasPara;
    }



    public String getPara() ...{
        return para;
    }



    public void setPara(String para) ...{
        this.para = para;
    }



    public Map<String, Object> getParas() ...{
        return paras;
    }



    public void setParas(Map<String, Object> paras) ...{
        this.paras = paras;
    }



    public String getValue() ...{
        return value;
    }



    public void setValue(String value) ...{
        this.value = value;
    }



    public void setAct(String act) ...{
        this.act = act;
    }





}

你可能感兴趣的:(jsp,Web,servlet)