• pagebean pagetag java 后台代码实现分页 demo 前台标签分页 后台java分页


    java 后台代码实现分页 demo 实力 自己写的 标签分页  

    package com.cszoc.sockstore.util;

    import java.util.HashMap;
    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.ServletActionContext;

    /**
    * 分页JavaBean
    *
    *
    */
    public class PageBean {
    /* 当前页 */
    private int curPage=1;

    /* 总记录数 */
    private int totalRecord = 0;

    /* 每页记录数 */
    private int pageRecord = 10;

    /* 是否分页,默认分页 */
    private boolean paginate = true;

    /* 请求的URL */
    private String url;

    /* 请求中的参数 */
    private Map<String, String[]> parameters = new HashMap<String, String[]>();

    public PageBean() {
    super();
    }

    public PageBean(HttpServletRequest request) {
    super();
    this.setRequest(request);
    }

    /**
    * 使用HttpServletRequest对象对分页Bean进行初始化
    *
    */
    @SuppressWarnings("unchecked")
    public void setRequest(HttpServletRequest request) {
    if(request.getAttribute("com.accp.book.util.PageBean")!=null)
    {
    PageBean p=(PageBean) request.getAttribute("com.accp.book.util.PageBean");
    copy(p);
    }
    else
    {
    this.setCurPage(request.getParameter("curPage"));// 从请求中获取当前页

    this.setPageRecord(request.getParameter("pageRecord"));// 从请求中获取页大小
    this.setPaginate(request.getParameter("paginate"));// 获取用来判断是否分页的参数
    // System.out.println("001");
    // System.out.println(request.getContextPath());
    // System.out.println(request.getServletPath());
    // System.out.println(this);
    // System.out.println(request);
    // System.out.println("001");
    this.setUrl(request.getContextPath() + request.getServletPath());// 获取表示请求资源的URL
    this.setParameters(new HashMap<String, String[]>(request
    .getParameterMap()));// 获取请求中的所有参数
    }
    request.setAttribute("com.accp.book.util.PageBean", this);// 将分页Bean保存request作用域当中
    }

    /**
    * true表示进行分页
    *
    * @return
    */
    public boolean isPaginate() {
    return paginate;
    }

    /**
    * 设置是否分页true分页,false不分页
    *
    * @param paginate
    */
    public void setPaginate(boolean paginate) {
    this.paginate = paginate;
    }

    /**
    * 设置是否分页true分页,false不分页
    *
    * @param paginate
    */
    public void setPaginate(String paginate) {
    if (null != paginate && paginate.trim().equalsIgnoreCase("false")) {
    this.setPaginate(false);
    } else {
    this.setPaginate(true);
    }
    }

    public Map<String, String[]> getParameters() {
    return parameters;
    }

    public void setParameters(Map<String, String[]> parameters) {
    this.parameters = parameters;
    }

    public String getUrl() {
    return url;
    }

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

    public int getCurPage() {
    return curPage;
    }

    public void setCurPage(int curPage) {
    if (curPage > 1) {
    this.curPage = curPage;
    }
    }

    public void setCurPage(String curPage) {
    if (null == curPage || curPage.trim().equals("")) {
    return;
    }
    int acurPage = Integer.parseInt(curPage);
    this.setCurPage(acurPage);
    }

    public int getPageRecord() {
    return pageRecord;
    }

    public void setPageRecord(int pageRecord) {
    this.pageRecord = pageRecord;
    }

    public void setPageRecord(String pageRecord) {
    if (null != pageRecord && !pageRecord.trim().equals("")) {
    this.pageRecord = Integer.parseInt(pageRecord);
    }
    }

    public int getTotalRecord() {
    return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
    this.totalRecord = totalRecord;
    }

    /**
    * 返回下一页页码
    *
    * @return
    */
    public int getNextPageNumber() {
    int nextPageNumber = this.curPage + 1;
    if (nextPageNumber > this.getMaxPageNumber()) {
    nextPageNumber = this.getMaxPageNumber();
    }
    return nextPageNumber;
    }

    /**
    * 返回上一页页码
    *
    * @return
    */
    public int getPreviousPageNumber() {
    int previousPageNumber = this.curPage - 1;
    if (previousPageNumber < 1) {
    previousPageNumber = 1;
    }
    return previousPageNumber;
    }

    /**
    * 返回最大页码编号
    *
    * @return
    */
    public int getMaxPageNumber() {
    int maxPageNumber = 1;
    if (this.totalRecord > this.pageRecord) {
    if (0 == this.totalRecord % this.pageRecord) {
    maxPageNumber = this.totalRecord / this.pageRecord;
    } else {
    maxPageNumber = this.totalRecord / this.pageRecord + 1;
    }
    }
    return maxPageNumber;
    }

    /**
    * 获取起始记录下标
    *
    * @return
    */
    public int getStartRecordIndex() {
    return this.pageRecord * (this.curPage - 1);
    }

    public String toString() {
    StringBuffer bf = new StringBuffer();
    bf.append("curPage=" + curPage);
    bf.append(", totalRecord=" + totalRecord);
    bf.append(", pageRecord=" + pageRecord);
    bf.append(", hashcode=" + hashCode());
    return bf.toString();
    }

    private void copy(PageBean p)
    {
    this.curPage=p.curPage;
    this.pageRecord=p.pageRecord;
    this.paginate=p.paginate;
    this.parameters=p.parameters;
    this.totalRecord=p.totalRecord;
    this.url=p.url;
    }

    }

    html 标签前台分页 

    --------------------------------------------------------------------------

    package com.cszoc.sockstore.util;

    import java.util.Iterator;
    import java.util.Map;

    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.BodyTagSupport;

    /**
    * 通用分页标签
    *
    */
    public class PageBeanTag extends BodyTagSupport {

    private static final long serialVersionUID = 8450962461296575662L;

    public PageBeanTag() {
    super();
    }

    @Override
    public int doStartTag() throws JspException {
    PageBean pageBean = (PageBean) pageContext.getRequest().getAttribute(
    "com.accp.book.util.PageBean");
    if (null != pageBean && pageBean.isPaginate()) {
    if(null!=pageContext.getRequest().getAttribute("reseturl"))
    {
    pageBean=(PageBean) pageContext.getRequest().getAttribute("reseturl");
    }
    try {
    // System.out.println("002");
    // System.out.println(pageBean.getUrl());
    // System.out.println(pageBean);
    // System.out.println("002");
    pageContext.getOut().write(toHTML(pageBean));
    } catch (Exception ex) {
    ex.printStackTrace();
    throw new RuntimeException(ex);
    }
    }
    pageContext.getRequest().removeAttribute("com.accp.book.util.PageBean");
    return SKIP_BODY;
    }

    /* 将分页JavaBean转换成HTML字符串 */
    private String toHTML(PageBean pageBean) {
    // 保存请求的参数到一个表单对象中,以供下次查询使用
    StringBuffer bf = new StringBuffer();
    bf.append("<form name='pageBeanForm' action='" + pageBean.getUrl()
    + "' method='post'>");
    bf.append("<input type='hidden' name='curPage' value=''/>");// 当前页是目前是空,但在点击分页按钮时会进行设置
    Map<String, String[]> parameters = pageBean.getParameters();// 请求中的所有参数
    String paramName = null;
    String[] paramValues = null;
    Iterator it = parameters.keySet().iterator();
    while (it.hasNext()) {
    paramName = (String) it.next();// 参数名
    if ("curPage".equals(paramName)) {
    continue;// 把当前页参数忽略掉
    }
    paramValues = parameters.get(paramName);// 参数值
    for (int i = 0; null != paramValues && i < paramValues.length; i++) {
    bf.append("<input type='hidden' name='" + paramName
    + "' value='" + paramValues[i] + "'/>");
    }
    }
    bf.append("</form>");

    // 显示分页按钮
    //bf.append("每页 " + pageBean.getPageRecord() + " 行");
    //bf.append(" 共 " + pageBean.getTotalRecord() + " 行");
    //bf.append(" 第 " + pageBean.getCurPage() + " 页");
    //bf.append(" 共 " + pageBean.getMaxPageNumber() + " 页");
    bf.append("<div class='page_div'><ul class='ulfleft ht_page'>");
    bf.append("<li class='ht_li1'>第"+pageBean.getCurPage()+"页/共"+pageBean.getMaxPageNumber()+"页</li><li class='ht_page_bg1'><a href='javascript:gotoPage(" + 1 + ")'>首页</a></li>"+" ");

    if(pageBean.getCurPage()!=1){
    bf.append(" "+" <li class='ht_page_bg1'><a href='javascript:gotoPage("
    + pageBean.getPreviousPageNumber() + ")'>上一页</a></li>"+" ");
    }




    if(pageBean.getCurPage()<=3)
    {
    if(pageBean.getMaxPageNumber()<=3)
    {
    for(int i=1;i<=pageBean.getMaxPageNumber();i++)
    {
    if(pageBean.getCurPage()==i)
    {
    bf.append(" "+" <li class='li_hover'><a href='javascript:gotoPage("+ i + ")'>"+i+"</a></li>"+" ");
    i+=1;
    }
    if(pageBean.getMaxPageNumber()>=i){
    bf.append(" "+" <li ><a href='javascript:gotoPage("
    + i + ")'>"+i+"</a></li>"+" ");
    }
    }
    }
    else
    {
    for(int i=1;i<=3;i++){
    if(pageBean.getCurPage()==i){
    bf.append("<li class='li_hover'>"+i+"</li>");
    i+=1;
    }
    if(pageBean.getMaxPageNumber()>=i){
    bf.append(" "+"<li > <a href='javascript:gotoPage("
    + i + ")'>"+i+"</a></li>"+" ");
    }
    }
    }

    }

    if(pageBean.getCurPage()<pageBean.getMaxPageNumber()-3&&pageBean.getCurPage()>3&&pageBean.getMaxPageNumber()>=3)
    {
    for(int i=pageBean.getCurPage()-2;i<=pageBean.getCurPage()+2;i++)
    {
    if(pageBean.getCurPage()==i)
    {
    bf.append("<li class='li_hover'>"+i+"</li>");
    i+=1;
    }
    bf.append(" "+" <li ><a href='javascript:gotoPage("
    + i + ")'>"+i+"</a></li>"+" ");
    }
    }


    if(pageBean.getCurPage()>=pageBean.getMaxPageNumber()-3&&pageBean.getMaxPageNumber()>=3&&pageBean.getCurPage()>3){
    for(int i=pageBean.getMaxPageNumber()-3;i<=pageBean.getMaxPageNumber();i++){
    if(pageBean.getCurPage()==i){
    bf.append("<li class='li_hover'>"+i+"</li>");
    i+=1;
    }
    if(pageBean.getMaxPageNumber()>=i){
    bf.append(" "+"<li > <a href='javascript:gotoPage("
    + i + ")'>"+" "+i+" "+"</a></li>"+" ");
    }
    }
    }



    if(pageBean.getCurPage()!=pageBean.getMaxPageNumber()){
    bf.append(" "+"<li class='ht_page_bg1'> <a href='javascript:gotoPage("
    + pageBean.getNextPageNumber() + ")'>下一页</a></li>"+" ");
    }

    bf.append(" "+" <li class='ht_page_bg1'><a href='javascript:gotoPage("
    + pageBean.getMaxPageNumber() + ")'>末页</a></li>");//</ul></div>
    bf.append(" "+" <li class='ht_page_bg1'><a href='javascript:jumpPage();'>跳转</a> </li>");
    bf.append(" "+" <li class='ht_page_bg2'>到<input type='text' class='fenye_txt' id='pageNumber'>页</li></ul></div>");

    //bf.append(" 页数 <input type='text' id='pageNumber' style='20px;' /> <a href='javascript:jumpPage()'>GO</a>");

    // 动态生成js
    bf.append("<script>");
    bf
    .append("function gotoPage(curPage){document.pageBeanForm.curPage.value=curPage;document.pageBeanForm.submit();}");
    bf.append("var maxPageNumber =" + pageBean.getMaxPageNumber() + ";");
    bf
    .append("function jumpPage(){var curPage=document.getElementById('pageNumber').value;if(curPage==''||isNaN(curPage)||parseInt(curPage)<1||parseInt(curPage)>maxPageNumber){alert('请输入一个正确的页码');return;}gotoPage(curPage);}");
    bf.append("</script>");

    return bf.toString();
    }
    }

  • 相关阅读:
    jenkins使用
    pytest+allure生成接口自动化测试报告
    charles系列
    go语言安装使用
    go语言介绍
    Nginx
    python面试题-python相关
    pyhon全栈开发学习目录
    补充【第二章】supervisor守护进程保障服务
    python全栈开发基础【补充】python中列表排序,字典排序,列表中的字典排序
  • 原文地址:https://www.cnblogs.com/hailun/p/5639558.html
Copyright © 2020-2023  润新知