zoukankan      html  css  js  c++  java
  • SSH自定义分页标签

    本文参考文章:http://blog.csdn.net/qjyong/article/details/3240303

    一、标签处理类:

    package cn.conris.sys.form;
    
    
    import java.io.IOException;  
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.jsp.JspException;  
    import javax.servlet.jsp.tagext.TagSupport;
    
    import cn.conris.sys.utils.PropertyUtil;
    
    public class PageTag  extends TagSupport {
             /**
              * 版本
              */
            private static final long serialVersionUID = 1L;
            
            private int pageSize = PropertyUtil.getPageSize(); // 每页要显示的记录数
            private int pageNo = PropertyUtil.getPageNumber(); // 页号
            private int recordCount; // 总记录数
            private String url; // 目的地URL
            
            public void setPageSize(int pageSize) {
                this.pageSize = pageSize;
            }
            public void setPageNo(int pageNo) {
                this.pageNo = pageNo;
            }
            public void setRecordCount(int recordCount) {
                this.recordCount = recordCount;
            }
            public void setUrl(String url) {
                this.url = url;
            }
            // 主要的逻辑
            @Override
            public int doStartTag() throws JspException {
                if(recordCount == 0){
                    return super.doStartTag();
                }
                
                // 总页数
                int pageCount = (recordCount + pageSize - 1) / pageSize;
                // 页号越界处理
                if (pageNo > pageCount) {
                    pageNo = pageCount;
                }
                if (pageNo < 1) {
                    pageNo = 1;
                }
                
                StringBuilder sb = new StringBuilder();
                
                sb.append("<form name='pageController' id='pageController' action='' method='post'>&nbsp")
                  .append("<input type='hidden' id='pageNo' name='pageNo' value='" + pageNo + "' />&nbsp");
                
                //------------------------------------ 获取所有請求中的参数
                HttpServletRequest request = (HttpServletRequest) pageContext
                        .getRequest();
                Enumeration<String> enumeration = request.getParameterNames();
                String name = null;
                String value = null;
                //把请求中的所有参数当作隐藏表单域在页面中写出)
                while (enumeration.hasMoreElements()) {
                    name =  enumeration.nextElement();
                    value = request.getParameter(name);
                    // 去除页号
                    if (name.equals("pageNo")) {
                        if (null != value && !"".equals(value)) {
                            pageNo = Integer.parseInt(value);
                        }
                        continue;
                    }
                    sb.append("<input type='hidden' name='")
                      .append(name)
                      .append("' value='")
                      .append(value)
                      .append("'/>&nbsp");
                }
                //----------------------------------------------------
                
                  
                sb.append(" 共" + pageCount + "页&nbsp&nbsp")
                  .append("当前第" + pageNo + "页 &nbsp&nbsp")
                  .append("共" + recordCount + "条 &nbsp&nbsp");
                
                if (pageNo == 1) {
                    sb.append("首页");
                    sb.append(" ");
                    sb.append("上一页&nbsp");
                } else {
                    sb.append("<a href='#' onclick='turnOverPage(1)'>首页</a>&nbsp");
                    sb.append(" ");
                    sb.append("<a href='#' onclick='turnOverPage(")
                            .append((pageNo - 1)).append(")'>上一页</a>&nbsp");
                }
                sb.append(" ");
                if (pageNo == pageCount) {
                    sb.append("下一页");
                    sb.append(" ");
                    sb.append("尾页&nbsp");
                } else {
                    sb.append("<a href='#' onclick='turnOverPage(")
                            .append((pageNo + 1)).append(")'>下一页</a>&nbsp");
                    sb.append(" ");
                    sb.append("<a href='#' onclick='turnOverPage(").append(pageCount)
                            .append(")'>尾页</a>&nbsp");
                }
                
                sb.append(" 跳到<select style='80px;' onChange='turnOverPage(this.value)'>&nbsp");
                for (int i = 1; i <= pageCount; i++) {
                    if (i == pageNo) {
                        sb.append("  <option  value='")
                          .append(i)
                          .append("' selected='selected'>第")
                          .append(i)
                          .append("页</option>&nbsp");
                    } else {
                        sb.append("  <option  value='")
                          .append(i)
                          .append("'>第")
                          .append(i)
                          .append("页</option>&nbsp");
                    }
                }
                sb.append("</select>&nbsp");
                sb.append(" &nbsp");
                sb.append("</form>&nbsp");
                
                // 生成提交表单的JS
                sb.append("<script language='javascript'>");
               // sb.append("  //翻页函数/t/n");
                sb.append("  function turnOverPage(no){");
                sb.append("    var form = document.pageController;");
               // sb.append("    //页号越界处理");
                sb.append("    if(no").append(">").append(pageCount).append(") {");
                sb.append("        no=").append(pageCount).append(";");
                sb.append("    }");
                sb.append("    if(no").append("< 1){");
                sb.append("        no=1;");
                sb.append("    }");
                sb.append("    form.").append("pageNo").append(".value=no;");
                sb.append("    form.action='").append(url).append("';");
                sb.append("    form.submit();");
                sb.append("  }");
                sb.append("</script>");
                
                try {
                    pageContext.getOut().println(sb.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                return super.doStartTag();
            }
    }

    二、标签库描述文件:

    <?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">
     <tlib-version>2.0</tlib-version>
     <short-name>my</short-name>
     <uri>http://conris/ModelSystem</uri>
     <tag>
        <description>通用分页标签</description>
        <name>pager</name>
        <tag-class>cn.conris.sys.form.PageTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
          <name>pageNo</name>
          <required>true</required>
          <rtexprvalue>true</rtexprvalue> 
        </attribute>
        <attribute>
          <name>recordCount</name>
          <required>true</required>
          <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
          <name>pageSize</name>
          <required>true</required>
          <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
          <name>url</name>
          <required>true</required>
          <rtexprvalue>true</rtexprvalue>
        </attribute>
      </tag>
    </taglib>

    三、PropertyUtil工具类:

    package cn.conris.sys.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropertyUtil {
        private static Properties p = new Properties();
        private static InputStream inputStream = null;
        static {
            inputStream = PropertyUtil.class
                    .getResourceAsStream("/page.properties");
            try {
                p.load(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static int getPageSize() {
            return Integer.parseInt(p.getProperty("pageSize"));
        }
    
        public static int getPageNumber() {
            return Integer.parseInt(p.getProperty("pageNumber"));
        }
    
        public static void main(String[] args) {
            System.out.println(getPageSize());
        }
    }

    四、page.properties配置文件:

    pageSize=5
    pageNumber=1

    五、action方法:

    /**
         * @Title: findAllPlaneLibrary
         * @Description: 查询所有飞机实体
         * @param @return 跳转到/entity_list.jsp
         * @return String 返回类型
         * @throws
         */
        public String findAllPlaneLibrary() {
            try {
                /** 查询飞机类型 **/
                List<EntityLibrary> entityLibraryList = entityLibraryService
                        .findEntityLibraryByBelongsLibrarycode("1000001");
                request.setAttribute("entityLibraryList", entityLibraryList);
                /**分页条件查询**/
                /**获取查询参数**/
                String name = planeLibrary.getName();
                String type = planeLibrary.getType();
                List<PlaneLibrary> planeLibraryList = planeLibraryService.findAll(name,type,speedStart,speedEnd);
                String pageNoStr = request.getParameter("pageNo");
                int pageNo = PropertyUtil.getPageNumber();
                if (pageNoStr != null && !pageNoStr.equals("")) {
                    pageNo = Integer.parseInt(pageNoStr);
                }
    
                int recordCount = planeLibraryList.size();
                int pageSize = PropertyUtil.getPageSize();
                int startNo = (pageNo - 1) * pageSize;
    
                List<PlaneLibrary> result = null;
                if (recordCount <= pageSize) {
                    result = planeLibraryList;
                } else {
                    if (startNo + pageSize > recordCount) {
                        result = planeLibraryList.subList(startNo, recordCount);
                    } else {
                        result = planeLibraryList.subList(startNo, startNo
                                + pageSize);
                    }
                }
                if (planeLibraryList != null && planeLibraryList.size() > 0) {
                    request.setAttribute("result", result);
                    request.setAttribute("recordCount", recordCount);
                    request.setAttribute("pageNo", pageNo);
                    request.setAttribute("pageSize", pageSize);
                    request.setAttribute("name", name);
                    request.setAttribute("type", type);
                    request.setAttribute("speedStart", speedStart);
                    request.setAttribute("speedEnd", speedEnd);
                }
            } catch (Exception e) {
                e.fillInStackTrace();
            }
            return "findAllPlaneLibrary";
        }

    七、Jsp页面配置

    添加引用:

    <%@ taglib uri="/mytaglib" prefix="mytag"%>

    遍历查询结果集合:

    <s:iterator value="#request.result" id="planeLibrary" status="i">
        <tr>
            <td class="center_td"><s:property value="#i.index+1" /></td>
            <td><a href="#"><img class="thumbnails" alt="#"
                    src="../<s:property value="#planeLibrary.imagepath"/>" width="64px"
                    height="64px" /> <s:property value="#planeLibrary.name" /> </a></td>
            <td><s:property value="#planeLibrary.type" /></td>
            <td><s:property value="#planeLibrary.speed" /></td>
            <td><s:property value="#planeLibrary.bombload" /></td>
            <td>4200</td>
            <td>
                <div class="btn-group">
                    <a href="../html/entity_view.html" class="btn" title="查看"><i
                        class="icon-file"></i> </a> <a href="../html/entity_add.html"
                        class="btn" title="修改"><i class="icon-pencil"></i> </a> <a
                        class="btn" title="删除"><i class="icon-remove"></i> </a>
                </div>
            </td>
        </tr>
    </s:iterator>

    查询条件回显:

    <form class="form-search" action="planeLibrary_findAllPlaneLibrary.action" method="post">
        飞机名称: <input type="text" class="input-medium span2" name="name" value="<s:property value="#request.name"/>">&nbsp;&nbsp;
        飞机类型: <select class="drop_down" name="type">
            <option value="全部">全部</option>
            <s:iterator value="#request.entityLibraryList" id="extendAttribute">
                 <s:if test="#request.type == #extendAttribute.name">
                      <option selected="selected" value="<s:property value="#extendAttribute.name"/>"><s:property value="#extendAttribute.name"/></option>
                 </s:if>
                 <s:else>
                      <option value="<s:property value="#extendAttribute.name"/>"><s:property value="#extendAttribute.name"/></option>
                 </s:else>
            </s:iterator>
        </select>&nbsp;&nbsp; 航速: <input type="text" name="speedStart" value="<s:property value="#request.speedStart"/>" class="input-medium span2">&nbsp;&nbsp;
        ~ <input name="speedEnd" type="text" value="<s:property value="#request.speedEnd"/>" class="input-medium span2">
        <button type="submit" class="btn">
            <i class="icon-search"></i> 搜索
        </button>
    </form>

    添加分页条:

    <div class="pagination pagination-centered">
            <mytag:pager pageNo="${pageNo}" recordCount="${recordCount}"
                  pageSize="${pageSize}" url="planeLibrary_findAllPlaneLibrary.action" />
    </div>

    八、注意

    该分页标签支持条件查询分页(需要将查询条件回显到jsp页面),点击分页条会对该条件进行分页显示。

    原理:点击分页条时,标签会将JSP页面中的所有文本框和下拉列表框转化成form表单提交。

  • 相关阅读:
    5个步骤迅速脱离低薪族
    phpdebugapd调试器
    重置zend studio 默认设置的方法
    CMD用什么命令可以删除服务?
    PHP自动识别字符集并完成转码
    一只赖蛤蟆写给白天鹅的情书
    遇到的问题
    Sql 注入漏洞
    分页技术
    flex学习记录——加载图片
  • 原文地址:https://www.cnblogs.com/dreammyle/p/3924373.html
Copyright © 2011-2022 走看看