首页 > 代码库 > SSH自定义分页标签

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.JspWriter;  import javax.servlet.jsp.tagext.SimpleTagSupport;  import javax.servlet.jsp.tagext.TagSupport;import cn.conris.sys.utils.PropertyUtil;public class PageTag  extends TagSupport {        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=http://www.mamicode.com/‘" + 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=http://www.mamicode.com/‘")                  .append(value)                  .append("‘/>&nbsp");            }            //----------------------------------------------------            sb.append(" 共" + pageCount + "页,&nbsp")              .append("当前第" + pageNo + "页 &nbsp");                        if (pageNo == 1) {                sb.append("首页");                sb.append(" ");                sb.append("上一页&nbsp");            } else {                sb.append("<a href=http://www.mamicode.com/‘#‘ onclick=‘turnOverPage(1)‘>首页 ");                sb.append(" ");                sb.append("<a href=http://www.mamicode.com/‘#‘ 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=http://www.mamicode.com/‘#‘ onclick=‘turnOverPage(")                        .append((pageNo + 1)).append(")‘>下一页</a>&nbsp");                sb.append(" ");                sb.append("<a href=http://www.mamicode.com/‘#‘ onclick=‘turnOverPage(").append(pageCount)                        .append(")‘>尾页</a>&nbsp");            }                        sb.append(" 跳到<select style=‘width:80px;‘ onChange=‘turnOverPage(this.value)‘>&nbsp");            for (int i = 1; i <= pageCount; i++) {                if (i == pageNo) {                    sb.append("  <option  value=http://www.mamicode.com/‘")                      .append(i)                      .append("‘ selected=‘selected‘>第")                      .append(i)                      .append("页</option>&nbsp");                } else {                    sb.append("  <option  value=http://www.mamicode.com/‘")                      .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=http://www.mamicode.com/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=5pageNumber=1

五、action方法:

/**     * @Title: findAllPlaneLibrary     * @Description: 查询所有飞机实体     * @param @return 跳转到/entity_list.jsp     * @return String 返回类型     * @throws     */    public String findAllPlaneLibrary() {        try {            List<PlaneLibrary> planeLibraryList = planeLibraryService.findAll();            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);            }        } catch (Exception e) {            e.fillInStackTrace();        }        return "findAllPlaneLibrary";    }

六、web.xml配置

  <!-- 配置自定义标签 -->  <jsp-config>    <taglib>        <taglib-uri>/mytaglib</taglib-uri>        <taglib-location>/WEB-INF/classes/pageTag.tld</taglib-location>    </taglib>  </jsp-config>

七、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="http://www.mamicode.com/#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>

添加分页条:

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