Package com.freewebsys.core.taglib.gwtstyle

Source Code of com.freewebsys.core.taglib.gwtstyle.PageTaglib

package com.freewebsys.core.taglib.gwtstyle;

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

import com.freewebsys.core.page.PageConf;

public class PageTaglib extends TagSupport {

  private String id;// 标识不同分页,当一个页面有多个分页时,
  private String action;
  private int start;
  private int limit;
  private int total;
  private String otherValue;
  private String theme;
  // 默认 分页跳转 js.兼容写法。兼容刷新分页 和 ajax分页。都调js函数。ajax要重写 goPage函数
  private static final String DEFAULT_PAGE_JS = "<script type='text/javascript'>function goPage(url){location.href = url;}</script>";
  // post 分页 js 使用post 分页 将支持查询分页,特殊字符& 和不能用get取得参数的分页
  private static final String POST_PAGE_JS = "<script type='text/javascript'>function goPage(url){var myForm = document.getElementById('goPageFormId');myForm.action = url;myForm.submit();}</script>";
  private static final String POST_PAGE_FORM_BEGIN = "<form id='goPageFormId' action='' method='post'>";
  private static final String POST_PAGE_FORM_END = "</form>";
  // 分页 div 开始
  private final String DEFAULT_THEME_BEGIN = "<div class='pagination' id='Pagination'>";
  private final String MINI_THEME_BEGIN = "<div class='paginationMini' id='PaginationMini'>";
  // 分页 div 结束
  private final String DEFAULT_THEME_END = "</div>";
  // 分页字符串
  private final String PAGE_A = "<a href=\"javaScript:void(0);\" onclick=\"goPage('";

  // 默认样式模板

  @Override
  public int doStartTag() throws JspException {
    try {
      if (total <= limit) {// 如果还不够一页 就不显示
        return super.doEndTag();
      }
      // 初始化参数.每次独立计算.否则分页有问题.
      PageConf pageConf = new PageConf(start, limit, total);
      String ctx = pageContext.getServletContext().getContextPath() + "/";
      //ctx += action + findOtherValue();
      if (action.indexOf("?") >= 0) {// 如果存在?号用&
        ctx += action + "&" + findOtherValue();
      } else {// 否则用?
        ctx += action + "?" + findOtherValue();
      }
      PageConf pageConfFirst = new PageConf(start, limit, total);
      String firstUrl = ctx;
      PageConf pageConfPrev = new PageConf(start, limit, total);
      String prevUrl = ctx;
      PageConf pageConfNext = new PageConf(start, limit, total);
      String nextUrl = ctx;
      PageConf pageConfLast = new PageConf(start, limit, total);
      String lastUrl = ctx;

      if (pageConf.hasPrevious()) {
        pageConfFirst.getFirst();
        firstUrl += "&start=" + pageConfFirst.getStart();
        // firstUrl += "-start~" + pageConfFirst.getStart();
        pageConfPrev.getPrevious();
        prevUrl += "&start=" + pageConfPrev.getStart();
        // prevUrl += "-start~" + pageConfPrev.getStart();
      }
      if (pageConf.hasNext()) {
        pageConfNext.getNext();
        nextUrl += "&start=" + pageConfNext.getStart();
        // nextUrl += "-start~" + pageConfNext.getStart();
        pageConfLast.getLast();
        lastUrl += "&start=" + pageConfLast.getStart();
        // lastUrl += "-start~" + pageConfLast.getStart();
      }
      // 直接将数据写到页面
      String str = genCenterButtons(start, limit, total, ctx);
      if (theme == null || theme.equals("")) {
        writeToPage(DEFAULT_PAGE_JS);// 默认跳转函数
        writeToPage(DEFAULT_THEME_BEGIN);
        writeToPage(str);
      } else if (theme.equals("post")) {// 执行post分页
        writeToPage(POST_PAGE_JS);// 默认跳转函数
        writeToPage(POST_PAGE_FORM_BEGIN);
        writeToPage(findOtherValueHidden());
        writeToPage(POST_PAGE_FORM_END);
        writeToPage(DEFAULT_THEME_BEGIN);
        writeToPage(str);
      } else if (theme.equals("ajax")) {// 普通ajax分页
        writeToPage(DEFAULT_THEME_BEGIN);
        writeToPage(str);
      } else if (theme.equals("miniAjax")) {// mini型ajax分页
        writeToPage(MINI_THEME_BEGIN);
        if (id != null) {// 标识不用分页
          str = str.replace("goPage", "goMiniPage_" + id);
        } else {
          str = str.replace("goPage", "goMiniPage");
        }
        writeToPage(str);
      }
      // 如果是ajax就要在页面重写goPage函数
      writeToPage(DEFAULT_THEME_END);

    } catch (Exception e) {
      e.printStackTrace();
    }
    return super.doEndTag();
  }

  /**
   * 生成多个跳转按钮方法,如1,2,3,4,5这样的按钮跳转最大7页跳转
   */
  private String genCenterButtons(int start, int limit, int total, String ctx) {

    // 初始化参数.每次独立计算.否则分页有问题.
    PageConf pageConf = new PageConf(start, limit, total);
    PageConf pageConfTemp1 = new PageConf(start, limit, total);
    PageConf pageConfTemp2 = new PageConf(start, limit, total);

    // 结束页
    PageConf pageConfTempLast = new PageConf(start, limit, total);
    //
    pageConfTempLast.getLast();
    // 字符串拼写.
    StringBuilder strOut = new StringBuilder();
    // 设置开始为当前页往前3页,最小1
    int begin = Math.max(1, (pageConf.getCurrentPage() + 1) - 3);
    // 设置结束为当前页往后3页,最大到总页数
    int end = Math.min((pageConf.getCurrentPage() + 1) + 3,
        pageConf.getTotalPage());
    int abs = 5 - Math.abs(pageConfTempLast.getCurrentPage()
        - pageConf.getCurrentPage());
    if (abs > 0) {
      begin = pageConf.getCurrentPage() - abs;
      begin = Math.min(begin, (pageConf.getCurrentPage() + 1) - 3);
      begin = Math.max(1, begin);
    }
    // 计算当前页和首页的距离
    abs = 7 - pageConf.getCurrentPage();
    if (abs > 0) {
      end = pageConf.getCurrentPage() + abs;
      end = Math.max((pageConf.getCurrentPage() + 1) + 3, end);
      end = Math.min(end, pageConf.getTotalPage());
    }

    if (pageConf.getTotalPage() > 7) {// 如果大于7显示首页末页
      // 显示前一页
      if (pageConfTemp1.hasPrevious()) {
        pageConfTemp1.getPrevious();
        strOut.append(PAGE_A + ctx + "&start="
            + pageConfTemp1.getStart() + "');\">");
        strOut.append("上一页</a>");
      }

      if (pageConf.getCurrentPage() > 3) {
        // 显示第 1
        strOut.append(PAGE_A + ctx + "&start=" + (0) + "');\">");
        strOut.append(1);
        strOut.append("</a>");
        if (pageConf.getCurrentPage() > 4) {
          // 显示第 2 页
          strOut.append(PAGE_A + ctx + "&start="
              + (0 + pageConf.getLimit()) + "');\">");
          strOut.append(2);
          strOut.append("</a>");
        }
        strOut.append("…");
      }
    }
    // if(pageConf.getCurrentPage())
    // 循环显示数字
    for (int i = begin; i <= end; i++) {
      // 循环多个table的button
      int startTemp = (i - 1) * pageConf.getLimit();
      if (i == (pageConf.getCurrentPage() + 1)) {
        strOut.append("<span class='current'>");
        strOut.append(i);// 当前页显示禁用
        strOut.append("</span>");
      } else {
        strOut.append(PAGE_A + ctx + "&start=" + startTemp + "');\">");
        strOut.append(i);
        strOut.append("</a>");
      }
    }
    // 末页显示,显示最后一页和倒数第二页
    if (pageConf.getTotalPage() > 7) {// 如果大于7显示首页末页

      if (pageConf.getCurrentPage() < (pageConfTempLast.getCurrentPage() - 3)) {
        // 如果当前页是倒数第4页就不显示
        strOut.append("…");
        // System.out.println(pageConf.getCurrentPage() + "/"
        // + pageConfTempLast.getCurrentPage());
        if (pageConf.getCurrentPage() < (pageConfTempLast
            .getCurrentPage() - 4)) {
          // 如果当前页是倒数第4页就不显示倒数第2页
          strOut.append(PAGE_A
              + ctx
              + "&start="
              + (pageConfTempLast.getStart() - pageConf
                  .getLimit()) + "');\">");
          strOut.append(pageConfTempLast.getCurrentPage());
          strOut.append("</a>");
        }
        strOut.append(PAGE_A + ctx + "&start="
            + pageConfTempLast.getStart() + "');\">");
        strOut.append(pageConfTempLast.getCurrentPage() + 1);
        strOut.append("</a>");
      }
      // 显示下一页
      if (pageConfTemp2.hasNext()) {
        pageConfTemp2.getNext();
        strOut.append(PAGE_A + ctx + "&start="
            + pageConfTemp2.getStart() + "');\">");
        strOut.append("下一页</a>");
      }
    }
    return strOut.toString();
  }

  /**
   * 添加其他参数.
   */
  private String findOtherValue() {
    String tempValue = "";
    if (theme != null && theme.equals("post")) {
      // 如果是post分页,不使用get方式传入其他参数用表单提交
      return "";
    }
    if (otherValue != null && !"".equals(otherValue)) {
      // 将数据按照','进行分开.
      String[] values = otherValue.split(",");
      for (int i = 0; i < values.length; i++) {
        // 从request里面取得数据value.
        String temp = (null == pageContext.getRequest().getParameter(
            values[i]) ? "" : pageContext.getRequest()
            .getParameter(values[i]));
        if (i == 0) {// 如果是第一个.直接等于.否则就添加&
          tempValue = values[i] + "=" + temp;
        } else {
          tempValue += "&" + values[i] + "=" + temp;
        }
        // tempValue += "-" + values[i] + "~" + temp;
      }
    }
    return tempValue;
  }

  /**
   * 添加其他参数.并存入hidden里面
   */
  private String findOtherValueHidden() {
    String tempValue = "";
    if (otherValue != null && !"".equals(otherValue)) {
      // 将数据按照','进行分开.
      String[] values = otherValue.split(",");
      for (int i = 0; i < values.length; i++) {
        // 从request里面取得数据value.
        String temp = (null == pageContext.getRequest().getParameter(
            values[i]) ? "" : pageContext.getRequest()
            .getParameter(values[i]));
        tempValue += "<input type='hidden' name='" + values[i]
            + "' value='" + temp + "'/>";
      }
    }
    return tempValue;
  }

  @Override
  public int doEndTag() throws JspException {
    try {
      // 读取模板
      // writeToPage("<!--GridPanelPage结束-->");// 完成一个h3不读取模板
    } catch (Exception e) {
      e.printStackTrace();
    }
    return super.doEndTag();
  }

  private void writeToPage(String strOut) {
    try {
      pageContext.getOut().print(strOut);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public int getStart() {
    return start;
  }

  public void setStart(int start) {
    this.start = start;
  }

  public int getLimit() {
    return limit;
  }

  public void setLimit(int limit) {
    this.limit = limit;
  }

  public String getOtherValue() {
    return otherValue;
  }

  public void setOtherValue(String otherValue) {
    this.otherValue = otherValue;
  }

  public String getAction() {
    return action;
  }

  public void setAction(String action) {
    this.action = action;
  }

  public int getTotal() {
    return total;
  }

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

  public String getTheme() {
    return theme;
  }

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

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

}
TOP

Related Classes of com.freewebsys.core.taglib.gwtstyle.PageTaglib

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.