首页 > 代码库 > java超强分页标签演示

java超强分页标签演示

最近在做一个项目,用到了一个分页,于是动手写了个分页标签,先将代码贴出来,供大家交流,写的不好,请见谅!、

以下是java标签类,继承自SimpleTagSupport

[java] view plaincopyprint?
  1. package com.lynn.oa.tag;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import javax.servlet.jsp.JspException;  
  8. import javax.servlet.jsp.JspWriter;  
  9. import javax.servlet.jsp.tagext.SimpleTagSupport;  
  10.   
  11. import com.lynn.oa.util.Page;  
  12.   
  13. public class PageTag extends SimpleTagSupport {  
  14.       
  15.     private Integer pageIndex;//当前页数  
  16.     private Integer pageCount;//总页数  
  17.     private String path;//页面链接  
  18.     private String param;//传入的参数  
  19.     //private List<Page> pageList;//显示的页号,一次显示10页,当前页位于中间第5的位置  
  20.       
  21.     @Override  
  22.     public void doTag() throws JspException, IOException {  
  23.           
  24.         JspWriter out = this.getJspContext().getOut();//指定输入流,用于页面输出分页信息、  
  25.         StringBuffer sb = new StringBuffer();//构建StringBuffer对象,用户拼接分页标签  
  26.         sb.append(this.getPageIndex());  
  27.         sb.append("/");  
  28.         sb.append(this.getPageCount());  
  29.         //显示当前页和总页数  
  30.           
  31.         //如果当前页在第一页,则首页和上一页没有超链接  
  32.         if(this.pageIndex==1){  
  33.             sb.append("首页 上一页 ");  
  34.         }else{  
  35.             sb.append("<a href=http://www.mamicode.com/‘");  
  36.             sb.append(this.path);  
  37.             sb.append("?");  
  38.             sb.append(this.param);  
  39.             sb.append("=");  
  40.             sb.append(1);  
  41.             sb.append("‘>首页</a> ");  
  42.             sb.append("<a href=http://www.mamicode.com/‘");  
  43.             sb.append(this.path);  
  44.             sb.append("?");  
  45.             sb.append(this.param);  
  46.             sb.append("=");  
  47.             sb.append(this.pageIndex-1);  
  48.             sb.append("‘>上一页</a> ");  
  49.               
  50.         }  
  51.         //下面的代码显示页码,当前页在中间位置,  
  52.         if(this.getPageCount()<=10){  
  53.             for (int i = 1; i <= this.getPageCount(); i++) {  
  54.             //如果页数小于等于10页,则全部显示  
  55.                   
  56.                     if( i == pageIndex){//如果页码等于当前页,则该页数没有超链接  
  57.                         sb.append("[");  
  58.                         sb.append(i);  
  59.                         sb.append("] ");  
  60.                     }else{//否则给出超链接  
  61.                         sb.append("<a href=http://www.mamicode.com/‘");  
  62.                         sb.append(this.path);  
  63.                         sb.append("?");  
  64.                         sb.append(this.param);  
  65.                         sb.append("=");  
  66.                         sb.append(i);  
  67.                         sb.append("‘>[");  
  68.                         sb.append(i);  
  69.                         sb.append("]</a> ");  
  70.                       
  71.                     }  
  72.                       
  73.                 }  
  74.         }else{//如果大于10页,则从当前页为中心只显示其中10页  
  75.                     int index=1;  
  76.             if(this.pageIndex>4){//并且如果当前页大于4页,从当前页前4页开始显示10个页数  
  77.                 for (int j = this.pageIndex-4;j<=this.getPageCount(); j++) {  
  78.                     if(j==this.pageIndex){//如果页码等于当前页,则该页数没有超链接  
  79.                         sb.append("[");  
  80.                         sb.append(j);  
  81.                         sb.append("] ");  
  82.                     }else{//否则给定超链接  
  83.                         sb.append("<a href=http://www.mamicode.com/‘");  
  84.                         sb.append(this.path);  
  85.                         sb.append("?");  
  86.                         sb.append(this.param);  
  87.                         sb.append("=");  
  88.                         sb.append(j);  
  89.                         sb.append("‘>[");  
  90.                         sb.append(j);  
  91.                         sb.append("]</a> ");  
  92.                     }  
  93.                     index++;  
  94.                     if(index>10)//如果循环到10次则退出循环  
  95.                         break;  
  96.                 }  
  97.                   
  98.                   
  99.             }else{  
  100.                 for (int i = 1; i <= this.getPageCount(); i++) {  
  101.                     //如果页数小于等于10页,则全部显示  
  102.                           
  103.                             if( pageIndex.equals(i)){//如果页码等于当前页,则该页数没有超链接  
  104.                                 sb.append("[");  
  105.                                 sb.append(i);  
  106.                                 sb.append("] ");  
  107.                             }else{//否则给出超链接  
  108.                                 sb.append("<a href=http://www.mamicode.com/‘");  
  109.                                 sb.append(this.path);  
  110.                                 sb.append("?");  
  111.                                 sb.append(this.param);  
  112.                                 sb.append("=");  
  113.                                 sb.append(i);  
  114.                                 sb.append("‘>[");  
  115.                                 sb.append(i);  
  116.                                 sb.append("]</a> ");  
  117.                               
  118.                             }  
  119.                             index++;  
  120.                             if(index>10)  
  121.                                 break;  
  122.                               
  123.                         }  
  124.             }  
  125.                   
  126.                   
  127.         }  
[java] view plaincopyprint?
  1.     //如果当前页是最后一页,则末页和下一页没有超链接  
  2.     if(this.pageIndex.equals(this.pageCount)){  
  3.         sb.append("下一页 末页 ");  
  4.     }else{  
  5.         sb.append("<a href=http://www.mamicode.com/‘");  
  6.         sb.append(this.path);  
  7.         sb.append("?");  
  8.         sb.append(this.param);  
  9.         sb.append("=");  
  10.         sb.append(this.pageIndex+1);  
  11.         sb.append("‘>下一页</a>");  
  12.         sb.append(" <a href=http://www.mamicode.com/‘");  
  13.         sb.append(this.path);  
  14.         sb.append("?");  
  15.         sb.append(this.param);  
  16.         sb.append("=");  
  17.         sb.append(this.pageCount);  
  18.         sb.append("‘>末页</a>");  
  19.     }  
  20.       
  21.     //System.out.prIntegerln(sb.toString());  
  22.   
  23.     //一下代码拼接转到第几页的js代码  
  24.     sb.append("<script type=‘text/javascript‘>");  
  25.     sb.append("function getPage(){");  
  26.     sb.append("var pageIndex = document.getElementById(‘pageText‘).value;");  
  27.     sb.append("window.location.href=http://www.mamicode.com/‘");  
  28.     sb.append(this.getPath());  
  29.     sb.append("?");  
  30.     sb.append(this.param);  
  31.     sb.append("=");  
  32.     sb.append("‘+pageIndex;}");  
  33.     sb.append("</script>");  
  34.     sb.append(" 转到第<input type=‘text‘ name=‘pageText‘ id=‘pageText‘ style=‘width:15px;‘ />页");  
  35.     sb.append("<input type=‘button‘ name=‘pageBtn‘ value=http://www.mamicode.com/‘GO‘ onclick=‘getPage()‘ />");  
  36.   
  37.     sb.append("<script type=‘text/javascript‘>");  
  38.     sb.append("$(function(){$(‘#pageSize‘).change(function(){var pageSize=$(‘#pageSize‘).val();window.location.href=http://www.mamicode.com/‘");  
  39.     sb.append(this.path);  
  40.     sb.append("?");  
  41.     sb.append("pageIndex=1&index=‘+pageSize});});</script>");  
  42.     sb.append("每页显示<select id=‘pageSize‘><option id=‘noone‘ value=http://www.mamicode.com/‘1‘>请选择");  
  43.     sb.append("<option value=http://www.mamicode.com/‘1‘>1");  
  44.     sb.append("<option value=http://www.mamicode.com/‘5‘>5");  
  45.     sb.append("<option value=http://www.mamicode.com/‘10‘>10页");  
  46.     out.print(sb);  
  47. }  
  48.   
  49. public Integer getPageIndex() {  
  50.     return pageIndex;  
  51. }  
  52.   
  53. public void setPageIndex(Integer pageIndex) {  
  54.     this.pageIndex = pageIndex;  
  55. }  
  56.   
  57. public Integer getPageCount() {  
  58.     return pageCount;  
  59. }  
  60.   
  61. public void setPageCount(Integer pageCount) {  
  62.     this.pageCount = pageCount;  
  63. }  
  64.   
  65. public String getPath() {  
  66.     return path;  
  67. }  
  68.   
  69. public void setPath(String path) {  
  70.     this.path = path;  
  71. }  
  72.   
  73. public String getParam() {  
  74.     return param;  
  75. }  
  76.   
  77. public void setParam(String param) {  
  78.     this.param = param;  
  79. }  
  80.   
  81.   
  82.   
  83. public static void main(String[] args) {  
  84.     PageTag pt = new PageTag();  
  85.     pt.setPageIndex(13);  
  86.     pt.setPageCount(13);  
  87.     //pt.setPageList(pageList)  
  88.     pt.setPath("index.jsp");  
  89.     pt.setParam("pageIndex");  
  90.       
  91.     try {  
  92.         pt.doTag();  
  93.     } catch (JspException e) {  
  94.         // TODO Auto-generated catch block  
  95.         e.printStackTrace();  
  96.     } catch (IOException e) {  
  97.         // TODO Auto-generated catch block  
  98.         e.printStackTrace();  
  99.     }  
  100. }  

tld文件如下:

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <taglib xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"  
  6.     version="2.1">  
  7.       
  8.   <description>JSTL 1.1 core library</description>  
  9.   <display-name>JSTL core</display-name>  
  10.   <tlib-version>1.0</tlib-version>  
  11.   <short-name>mt</short-name>  
  12.   <uri>http://www.lynn88.com/jsp/jstl/pagetag</uri>  
  13.   
  14.   <tag>  
  15.     <description>  
  16.         Catches any Throwable that occurs in its body and optionally  
  17.         exposes it.  
  18.     </description>  
  19.     <name>page</name>  
  20.     <tag-class>com.lynn.oa.tag.PageTag</tag-class>  
  21.     <body-content>empty</body-content>  
  22.     <attribute>  
  23.           
  24.         <name>pageIndex</name>  
  25.         <required>true</required>  
  26.         <rtexprvalue>true</rtexprvalue>  
  27.         <type>java.lang.Integer</type>  
  28.     </attribute>  
  29.     <attribute>  
  30.           
  31.         <name>pageCount</name>  
  32.         <required>true</required>  
  33.         <rtexprvalue>true</rtexprvalue>  
  34.         <type>java.lang.Integer</type>  
  35.     </attribute>  
  36.      <attribute>  
  37.           
  38.         <name>path</name>  
  39.         <required>true</required>  
  40.         <rtexprvalue>true</rtexprvalue>  
  41.         <type>java.lang.String</type>  
  42.     </attribute>  
  43.     <attribute>  
  44.           
  45.         <name>param</name>  
  46.         <required>true</required>  
  47.         <rtexprvalue>true</rtexprvalue>  
  48.         <type>java.lang.String</type>  
  49.     </attribute>  
  50.   </tag>  
  51.   
  52. </taglib>