自定义分页标签
自定义分页标签
black_lxf_720 发表于2年前
自定义分页标签
  • 发表于 2年前
  • 阅读 26
  • 收藏 2
  • 点赞 1
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: 基于form表单的自定义分页标签
public class PageTag extends TagSupport {
 private static final long serialVersionUID = -6295188152350309742L; 
 //起到与jsp页面之间的通讯作用,set方法可以将jsp页面的pageContext对象传递给标签处理器
 private PageContext pageContext;
 // 当前页
 private Integer pageNum;
 // 每页的数量
 private Integer pageSize;
 // 总记录数
 private Integer total;
 // 总页数
 private Integer pages;
 private String seachForm;
 private String paramEncoding = "UTF-8";
 /**
  * page bean的属性名
  */
 private String pageBeanName;
 public String getPageBeanName() {
  return pageBeanName;
 }
 public void setPageBeanName(String pageBeanName) {
  this.pageBeanName = pageBeanName;
 }
 public Integer getPageNum() {
  return pageNum;
 }
 public void setPageNum(Integer pageNum) {
  this.pageNum = pageNum;
 }
 public int getPageSize() {
  return pageSize;
 }
 public void setPageSize(Integer pageSize) {
  this.pageSize = pageSize;
 }
 public long getTotal() {
  return total;
 }
 public void setTotal(Integer total) {
  this.total = total;
 }
 public Integer getPages() {
  return pages;
 }
 public void setPages(Integer pages) {
  this.pages = pages;
 }
 public String getSeachForm() {
  return seachForm;
 }
 public void setSeachForm(String seachForm) {
  this.seachForm = seachForm;
 }
 public String getParamEncoding() {
  return paramEncoding;
 }
 public void setParamEncoding(String paramEncoding) {
  this.paramEncoding = paramEncoding;
 }
 
 public PageContext getPageContext() {
  return pageContext;
 }
 
 public void setPageContext(PageContext pageContext) {
  this.pageContext = pageContext;
 }
 @Override
 public int doStartTag() throws JspException {
  if (pageBeanName == null)
   pageBeanName = "pagination";
  Pagination pagination = (Pagination) pageContext.getRequest().getAttribute(pageBeanName);
  if (pagination != null) {
   pageNum = pagination.getPageNum();
   pages = pagination.getTotalPage();
  }
  return 0;
 }
 @Override
 public int doEndTag() throws JspException {
  JspWriter out = pageContext.getOut();
  try {
   out.write(buildPage());
  } catch (IOException e) {
   e.printStackTrace();
   throw new RuntimeException(e);
  }
  return 0;
 }
 /**
  * @Title: buildPage 
  * @Description:创建分页
  * @return 
  * @author 
  * @date 2016年1月22日
  */
 private String buildPage() {
  if (pages == 1) {
   return "";
  }
  int start = 0, end = 10;
  if (pageNum >= 10 || (pageNum > 5 && pages > 10))
   start = pageNum - 5;
  if (pages > pageNum + 5)
   end = pageNum + 5;
  else
   end = pages;
  StringBuffer sb = new StringBuffer();
  sb.append("<nav class=\"pull-right pull-right-1\">");
  sb.append("<ul class=\"pagination\">");
  if (pageNum > 1)
   sb.append("<li class=\"previous\"><a href=\"javascript:void(0)\" onclick=" + getPageMethod(pageNum - 1) + ">上一页</a></li>");
  if (start > 0)
   sb.append("<li class='nomal'  ><a href='javascript:void(0)' onclick=" + getPageMethod(1) + " >" + 1 + "</a></li>");
  for (int i = start; i < end; i++) {
   if ((i + 1) == pageNum)
    sb.append("<li class='active'><a href='javascript:void(0)' onclick=" + getPageMethod(i + 1) + " >" + (i + 1) + "</a></li>");
   else
    sb.append("<li class='nomal'><a href='javascript:void(0)' onclick=" + getPageMethod(i + 1) + ">" + (i + 1) + "</a></li>");
  }
  if (pages > 10 && pageNum < pages - 4)
   sb.append("<li class='nomal'><a href='javascript:void(0)' onclick=" + getPageMethod(pages) + ">" + pages + "</a></li>");
  if (pageNum != pages && pages != 0)
   sb.append("<li class=\"next\"><a href=\"javascript:void(0)\" onclick=" + getPageMethod(pageNum + 1) + ">下一页</a></li>");
  sb.append("</ul>");
  sb.append("</nav>");
  return sb.toString();
 }
 /**
  * @Title: getPageMethod 
  * @Description:构建分页的方法名
  * @param pageNum
  * @return 
  * @author 
  * @date 2016年1月22日
  */
 private String getPageMethod(int pageNum) {
  return "\"pageJunmp('" + seachForm + "'," + pageNum + ",'" + pageBeanName + "')\"";
 }
}
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
    <description>bootcss3 分页</description>
    <tlib-version>1.0</tlib-version>
    <short-name>page</short-name>
    <uri>http://mycompany.com/tag/page</uri>
    <tag>
        <name>pager</name>
        <tag-class>com.mycompany.website.unit.PageTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>seachForm</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>pageBeanName</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>paramEncoding</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
function pageJunmp(form,pageNum,pageBeanName){
  var inputName = pageBeanName+".pageNum";
  form=$("#"+form);
  form.attr("method","post");
  var input = "<input type='hidden' id='pageNum' value='"+pageNum+"' name='"+inputName+"'>";
  form.append(input);
  form.submit();
}
<%@taglib prefix="p" uri="http://szmsd.com/tag/page" %>//引入标签
<p:pager seachForm="searchForm" />//标签使用


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 5
博文 36
码字总数 20281
×
black_lxf_720
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: