运用第一篇分页的例子,结果以失败告终。在网上又寻找了很多例子。大多是都是这一种。着手开发的项目采用spring MVC框架。符合需求。摘下来,试了一试。
网上的例子总是少一些东西。经过一番折腾。终于搞定了。下面分享出来,供参考。
分页基本逻辑思想懂了,重点是在于怎么实现。怎么实现更好。还需要进一步思考。
配置xml-pager.tld
<?xml version="1.0" encoding="UTF-8" ?> <taglib 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" version="2.0"> <description>Pager</description> <tlib-version>1.0</tlib-version> <short-name>page</short-name> <uri></uri> <tag> <name>createPager</name> <tag-class>getui.util.Pager</tag-class> <body-content>JSP</body-content> <attribute> <name>curPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>totalPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>pageSize</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>totalCount</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>formId</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
分页控件-Pager
package getui.util; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /*** * 分页控件 * * @author Anny */ public class Pager extends TagSupport { private Integer curPage; //当前页码 private Integer totalPage; //总页数 private Integer pageSize = 10; //一页显示的记录数 private Integer totalCount = 0; //记录总数 private String formId; //请求的action name private Integer lastIndex; //结束索引 public int doStartTag() throws JspException { StringBuffer buffer=new StringBuffer(); JspWriter out = pageContext.getOut(); int pageNumber = 0; if (totalPage % pageSize == 0) { pageNumber = totalPage / pageSize; } else { pageNumber = (totalPage / pageSize) + 1; } if (curPage < 1) { curPage = 1; } try { if (pageNumber > 0) { buffer.append("<script type='text/javascript'>");//script-Start buffer.append("function go(pageNum)"); buffer.append("{");//{start buffer.append("var f = document.getElementById('" + formId + "');"); buffer.append("f.action = f.action + '?pageNum=' + pageNum + '&pageSize="+pageSize + "';"); buffer.append("f.submit();" ); buffer.append("}");//}end buffer.append("</script>");//script-end out.print(buffer.toString()); out.append("<div> ");//page-number-strip out.print("<ul>");//添加Bootstrap分页的样式pagination int start = 1; int end = totalPage; for (int i = 4; i >= 1; i--) { if ((curPage - i) >= 1) { start = curPage - i; break; } } for (int i = 4; i >= 1; i--) { if ((curPage + i) <= totalPage) { end = curPage + i; break; } } // 如果小于9则右侧补齐 if (end - start + 1 <= 9) { Integer padLen = 9 - (end - start + 1); for (int i = padLen; i >= 1; i--) { if ((end + i) <= totalPage) { end = end + i; break; } } } // 如果还小于9左侧补齐 if (end - start + 1 <= 9) { Integer padLen = 9 - (end - start + 1); for (int i = padLen; i >= 1; i--) { if ((start - i) >= 1) { start = start - i; break; } } } if (curPage > 1) { if (start > 1) { out.print("<li><a href='javascript:go(1)'>首页</a></li>"); } out.print("<li><a href='javascript:go(" + (curPage - 1) + ")'>上一页</a></li>"); } for (int i = start; i <= end; i++) { if (i == curPage) { out.print("<li><a href='javascript:void(0);'>" + i + "</a></li>"); } else { out.print("<li><a href='javascript:go(" + i + ")'>" + i + "</a></li>"); } } if (curPage < totalPage) { out.print("<li><a href='javascript:go(" + (curPage + 1) + ")'>下一页</a></li>"); if (end < totalPage) { out.print("<li><a href='javascript:go(" + totalPage + ")'>尾页</a></li>"); } } out.print("<li><a href='javascript:void(0)'>共" + totalPage + "页" + this.totalCount + "条</a></li>"); out.print("</ul>"); out.print("</div>"); } } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } /** * 算开始索引 * * @param pageNum - * @param pageSize * @return */ public static Integer getStartIndex(Integer pageNum, Integer pageSize) { Integer res = 0; if (pageNum > 0) { res = (pageNum - 1) * pageSize; } return res; } /** * 算结束索引 -- 方法暂时未用 * * @param pageSize * @param totalCount * @param totalPage * @param pageNum * @return */ public static Integer getLastIndex(Integer pageSize,Integer totalCount,Integer totalPage,Integer pageNum){ //计算结束时候的索引 Integer lastIndex =0; if( totalCount < pageSize){ lastIndex = totalCount; }else if((totalCount % pageSize == 0) || (totalCount % pageSize != 0 && pageNum < totalPage)){ lastIndex = pageNum * pageSize; }else if(totalCount % pageSize != 0 && pageNum == totalPage){//最后一页 lastIndex = totalCount ; } return lastIndex; } public Integer getLastIndex() { return lastIndex; } public void setLastIndex(Integer lastIndex) { this.lastIndex = lastIndex; } public void setCurPage(Integer curPage) { this.curPage = curPage; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public void setFormId(String formId) { this.formId = formId; } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } }
分页action集成类-BaseController