jsp自定义分页标签

    技术2025-02-27  44

    废话一句不说,看看我的步骤(写的不好,仅供参考)

    1.定义分页辅助实体类PageBean.java

    2.定义分页标签PageTag.java & page.tld

    3.定义分页辅助的js文件PageHelper.js

    4.准备标签的样式

    5.引用PageBean进行数据库的查询

    6.引用自定义标签绑定到页面

    代码按步骤顺序如下:

    PageBean.java

     

     package com.hbjy.common; import java.util.List; /** * 分页辅助类 * @author xiash * */ public class PageBean { /** * 当前页的所有数据 */ private List result; /** * 页前页第一条数据的下标 */ private int currentPageIndex; /** * 当前页码 */ private int currentPageNo = 1; /** * 下一页页码 */ private int nextPageNo; /** * 上一页页码 */ private int prePageNo; /** * 第一页页码 */ private int firstPageNo; /** * 最后一页页码 */ private int lastPageNo; /** * 页的大小 */ private int pageSize = 10; /** * 总共记录数 */ private int rowsCount; /** * 总共页数 */ private int pageCount; /** * 排顺序名称 */ private String orderByName; /** * 排顺序方式(升 降) */ private String orderByMethod = "asc"; public PageBean() { } /** * 构造函数 * @param rowsCount * @param pageSize */ public PageBean(int rowsCount,int pageSize) { this.rowsCount = rowsCount; this.pageSize = pageSize; } public int getCurrentPageNo() { return currentPageNo; } public void setCurrentPageNo(int currentPageNo) { this.currentPageNo = currentPageNo; } /** * 得到下一页码 * @return */ public int getNextPageNo() { this.nextPageNo = this.currentPageNo + 1; if(this.nextPageNo>this.getPageCount()) { this.nextPageNo = this.getPageCount(); } return nextPageNo; } /** * 得到总共页数 * @return */ public int getPageCount() { if(this.rowsCount % this.pageSize == 0) { this.pageCount = this.rowsCount / this.pageSize; } else { this.pageCount = (this.rowsCount / this.pageSize) + 1; } return pageCount; } /** * 得到上一页 * @return */ public int getPrePageNo() { this.prePageNo = this.currentPageNo - 1; if(this.prePageNo <=0 ) { this.prePageNo = 1; } return prePageNo; } /** * 得到第一页码 * @return */ public int getFirstPageNo() { this.firstPageNo = 1; return firstPageNo; } /** * 得到最后一页 * @return */ public int getLastPageNo() { this.lastPageNo = this.getPageCount(); return lastPageNo; } public String getOrderByName() { return orderByName; } public void setOrderByName(String orderByName) { this.orderByName = orderByName; } public String getOrderByMethod() { return orderByMethod; } public void setOrderByMethod(String orderByMethod) { this.orderByMethod = orderByMethod; } public int getPageSize() { return pageSize; } public int getRowsCount() { return rowsCount; } public int getCurrentPageIndex() { this.currentPageIndex = (this.getCurrentPageNo()-1) * pageSize; return this.currentPageIndex; } public List getResult() { return result; } public void setResult(List result) { this.result = result; } public void setRowsCount(int rowsCount) { this.rowsCount = rowsCount; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * 当前页是否是第一页 * @return */ public boolean getIsFirst() { return this.getCurrentPageNo() == 1; } /** * 当前页是否是最后一页 * @return */ public boolean getIsLast() { return this.getCurrentPageIndex() == this.getLastPageNo(); } }

     

    PageTag.java

     

     package com.hbjy.common; import java.io.IOException; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport; public class PageTag extends TagSupport { private String pageCss; public String getPageCss() { return pageCss; } public void setPageCss(String pageCss) { this.pageCss = pageCss; } /** * 页前页第一条数据的下标 */ private int currentPageIndex; /** * 当前页码 */ private int currentPageNo = 1; /** * 下一页页码 */ private int nextPageNo; public int getCurrentPageIndex() { return currentPageIndex; } public void setCurrentPageIndex(int currentPageIndex) { this.currentPageIndex = currentPageIndex; } public int getCurrentPageNo() { return currentPageNo; } public void setCurrentPageNo(int currentPageNo) { this.currentPageNo = currentPageNo; } public int getNextPageNo() { return nextPageNo; } public void setNextPageNo(int nextPageNo) { this.nextPageNo = nextPageNo; } public int getPrePageNo() { return prePageNo; } public void setPrePageNo(int prePageNo) { this.prePageNo = prePageNo; } public int getFirstPageNo() { return firstPageNo; } public void setFirstPageNo(int firstPageNo) { this.firstPageNo = firstPageNo; } public int getLastPageNo() { return lastPageNo; } public void setLastPageNo(int lastPageNo) { this.lastPageNo = lastPageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getRowsCount() { return rowsCount; } public void setRowsCount(int rowsCount) { this.rowsCount = rowsCount; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } /** * 上一页页码 */ private int prePageNo; /** * 第一页页码 */ private int firstPageNo; /** * 最后一页页码 */ private int lastPageNo; /** * 页的大小 */ private int pageSize = 10; /** * 总共记录数 */ private int rowsCount; /** * 总共页数 */ private int pageCount; @Override public int doEndTag() throws JspException { // TODO Auto-generated method stub return this.EVAL_PAGE; } @Override public int doStartTag() throws JspException { // TODO Auto-generated method stub JspWriter out=pageContext.getOut(); StringBuffer result = new StringBuffer(""); result.append("<div class=/"").append(this.pageCss).append("/">"); result.append("<input type=/"hidden/" name=/"firstPageNo/" value=/"").append(this.firstPageNo).append("/" />"); result.append("<input type=/"hidden/" name=/"lastPageNo/" value=/"").append(this.lastPageNo).append("/" />"); result.append("<input type=/"hidden/" name=/"prePageNo/" value=/"").append(this.prePageNo).append("/" />"); result.append("<input type=/"hidden/" name=/"nextPageNo/" value=/"").append(this.nextPageNo).append("/" />"); result.append("总计 <span style="/" mce_style="/""color: red;/">").append(this.rowsCount).append("</span>条记录"); result.append("分为 <span style="/" mce_style="/""color: red;/">").append(this.pageCount).append("</span> 页 "); result.append("当前第 <span style="/" mce_style="/""color: red;/">").append(this.currentPageNo).append("</span> 页 "); if(this.currentPageNo <= this.firstPageNo){ result.append("<font style="/" mce_style="/""color: gray; text-decoration: underline/">第一页</font>  "); result.append("<font style="/" mce_style="/""color: gray; text-decoration: underline/">上一页</font>  "); }else{ result.append("<a href="/" mce_href="/""javascript:doFirst();/">第一页</a>  "); result.append("<a href="/" mce_href="/""javascript:doPre();/">上一页</a>  "); } if(this.currentPageNo >= this.lastPageNo){ result.append("<font style="/" mce_style="/""color: gray; text-decoration: underline/">下一页</font>  "); result.append("<font style="/" mce_style="/""color: gray; text-decoration: underline/">最后一页</font>  "); }else{ result.append("<a href="/" mce_href="/""javascript:doNext();/">下一页</a>  "); result.append("<a href="/" mce_href="/""javascript:doLast();/">最后一页</a>"); } result.append("转到<input name=/"p/" value=/"").append(this.currentPageNo).append("/" size=/"2/" class=/"FormStyle2/" /> 页 "); result.append("<input type=/"button/" width=/"20px;/" οnclick=/"go();/" class=/"pageGo/" value=/"GO/" /></div>"); try { out.write(result.toString()); } catch (IOException e) { System.out.println("在标签处理类输出的时候出现异常"); e.printStackTrace(); } return this.EVAL_BODY_INCLUDE; } }

     

    page.tld

     

     <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems,Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.2</jspversion> <shortname>jykj</shortname> <tag> <name>page</name> <tag-class>com.hbjy.common.PageTag</tag-class> <body-content>empty</body-content> <attribute> <name>pageCount</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>rowsCount</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>currentPageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>nextPageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>prePageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>firstPageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>lastPageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pageCss</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>

     

    PageHelper.js

     

    function doNext() { document.forms[0].p.value = document.forms[0].nextPageNo.value; document.forms[0].submit(); } function doPre() { document.forms[0].p.value = document.forms[0].prePageNo.value; document.forms[0].submit(); } function doFirst() { document.forms[0].p.value = document.forms[0].firstPageNo.value; document.forms[0].submit(); } function doLast() { document.forms[0].p.value = document.forms[0].lastPageNo.value; document.forms[0].submit(); } function go() { // document.forms[0].p.value = ${pr.lastPageNo}; var p = document.forms[0].p.value; if (p > document.forms[0].lastPageNo.value) { document.forms[0].p.value = document.forms[0].lastPageNo.value; } if (p < document.forms[0].firstPageNo.value) { document.forms[0].p.value = document.forms[0].firstPageNo.value; } document.forms[0].submit(); }

     

    PageCss

     

    .pageGo{ border: #bfbebe 1px solid; background: #fff; width: 40px; height: 21px; line-height: 20px; } .page_div{ width:100%; margin-top:2px; background-color:#E6E6E6; float:left; color:#5c5b5b; height:34px; vertical-align:text-bottom; line-height:34px; text-align: center; padding-top: 4px; border:1px solid #D1D1D1; } .page_div a{ color:blue; text-decoration:underline; } .page_div a:hover{ color:red; text-decoration:underline; }

     

    数据访问

     

    ---------------- 控制层传入pagebean------------- /**申明分页辅助对象pagebean,并设置分页参数*/ PageBean page = new PageBean(); String p = request.getParameter("p"); p = (p == null ? "1":p); page.setCurrentPageNo(Integer.parseInt(p)); /**执行条件查询*/ List list = deptBusiness.findAllDepts(page, dept); /**封装数据分页参数及查询条件传至页面*/ request.setAttribute("pr", page); ---------持久层封装pagebean(示例为ibatis数据访问)----------String rowCount = (String)this.getSqlMapClientTemplate().queryForObject("findDeptCount",dept); page.setRowsCount(Integer.parseInt(rowCount)); if(page.getCurrentPageNo() >page.getLastPageNo()){ page.setCurrentPageNo(page.getLastPageNo()); } if(page.getCurrentPageNo() <page.getFirstPageNo()){ page.setCurrentPageNo(page.getFirstPageNo()); } List list = this.getSqlMapClientTemplate().queryForList("findAllDept", dept, (page.getCurrentPageNo()-1)*page.getPageSize(), page.getPageSize()); page.setResult(list);

     

    页面绑定:根据js页面需要form表单并且数据放在 forms[0]中

     

     <!-- --------------头导入文件-------- --> <%@ taglib uri="/WEB-INF/tld/page.tld" prefix="page" %> <link rel="stylesheet" href="theme/css/main.css" mce_href="theme/css/main.css" type="text/css"></link> //包含page样式 <mce:script type="text/javascript" src="theme/js/PageHelper.js" mce_src="theme/js/PageHelper.js"></mce:script> <!-- -------中间为数据显示------------- --> <!------------自定义标签------------- --> <page:page prePageNo="${ pr.prePageNo }" pageCss="page_div" rowsCount="${ pr.rowsCount }" pageCount="${ pr.pageCount }" firstPageNo="${ pr.firstPageNo }" nextPageNo="${ pr.nextPageNo }" lastPageNo="${ pr.lastPageNo }" currentPageNo="${ pr.currentPageNo }"/>

     

    OK 差不多了吧,自己写的,可能不太智能,有什么遗漏或不清楚的请留言!

    最新回复(0)