zoukankan      html  css  js  c++  java
  • (转)java web自定义分页标签

    转载至http://liuxi1024.iteye.com/blog/707784

    效果如图:


     

     1、JSP规范1.1版本后增加了自定义标签库。实现自定义标签的步骤

    (1)开发自定义标签处理类。

    (2)建立*.tld文件。

    (3)在web.xml中增加自定义标签的定义。

    (4)在jsp中使用自定义标签。

    2、自定义标签类

    (1)继承javax.servlet.jsp.tagext.TagSupport

    (2)标签类属性,及每个属性的getter和setter方法

    (3)重写doStartTag或doEndTag方法。当jsp解析这个标签的时候,在“<”处触发 doStartTag 事件,在“>”时触发 doEndTag 事件。通常在 doStartTag 里进行初始化,流程选择操作,在 doEndTag 里后续页面输出控制。

    Java代码  收藏代码
    1. import java.io.IOException;  
    2.   
    3. import javax.servlet.jsp.JspException;  
    4. import javax.servlet.jsp.tagext.TagSupport;  
    5.   
    6. import org.apache.commons.logging.Log;  
    7. import org.apache.commons.logging.LogFactory;  
    8.   
    9. /** 
    10.  *  
    11.  * @author liuxi 
    12.  */  
    13.   
    14. public class PageThirdTag extends TagSupport {  
    15.   
    16.     private static final Log log = LogFactory.getLog(PageTwoTag.class);  
    17.   
    18.     private String formName;  
    19.       
    20.     private String curPage;  
    21.       
    22.     private String showPages;  
    23.       
    24.     private String totalPages;  
    25.       
    26.     private String PREVIOUS_PAGE = "上一页";  
    27.   
    28.     private String NEXT_PAGE = "下一页 ";  
    29.       
    30.     public String getHref(int number) {  
    31.         return "Javascript:ToPage(" + number + ");";  
    32.     }  
    33.   
    34.     public String goHref(int number) {  
    35.         return " <a href="" + getHref(number) + "" class="pagebox">" + number + "</a>";  
    36.     }  
    37.       
    38.     public int doEndTag() throws JspException {  
    39.           
    40.           
    41.         int showPages = Integer.parseInt(this.showPages);  
    42.         int curpage = Integer.parseInt(this.curPage);  
    43.         int totalPages = Integer.parseInt(this.totalPages);  
    44.           
    45.         StringBuffer strBuf = new StringBuffer(512);  
    46.         // 总页数  
    47.         int pagecount = totalPages;  
    48.         // 初始化值  
    49.         if (curpage == 0) {  
    50.             curpage = 1;  
    51.         } else {  
    52.             if (curpage <= 0) {  
    53.                 curpage = 1;  
    54.             }  
    55.             if (curpage > pagecount) {  
    56.                 curpage = pagecount;  
    57.             }  
    58.         }  
    59.           
    60.         strBuf.append("<style type='text/css'>");  
    61.         strBuf.append(".pagebox{margin-left:2px;padding:3px 5px 3px 5px; border:1px solid #fff; background-color:#ebebeb;color:#FFFFFF; font-size:12px;}");  
    62.         strBuf.append(".cpagebox{margin-left:2px;padding:3px 5px 3px 5px; border:1px gray; background-color:#ebebeb; color:red; font-size:12px;}");  
    63.         strBuf.append(".vpagebox{margin-left:2px;padding:3px 5px 3px 5px; background-color:#FFFFFF; color:#000000;font-size:12px;}");  
    64.         strBuf.append("</style>");  
    65.           
    66.         strBuf.append("<script language='JavaScript' type='text/JavaScript'>");  
    67.         strBuf.append("function ToPage(p) {  ");  
    68.         strBuf.append(" window.document." + formName + ".pageNo.value=p; ");  
    69.         strBuf.append(" window.document." + formName + ".submit(); ");  
    70.         strBuf.append("}</script>");  
    71.           
    72.         if (curpage > 1) {  
    73.             strBuf.append("<a href="" + getHref(curpage - 1) + "" class="pagebox" >" + PREVIOUS_PAGE + "</a>");  
    74.         }  
    75.   
    76.         // 分页  
    77.         if (pagecount <= showPages + 2) {  
    78.             for (int i = 1; i <= pagecount; i++) {  
    79.                 if (i == curpage) {  
    80.                     strBuf.append("<font class="cpagebox">" + i + "</font>");  
    81.                 } else {  
    82.                     strBuf.append(goHref(i));  
    83.                 }  
    84.             }  
    85.         } else {  
    86.             if (curpage < showPages) {   
    87.                 for (int i = 1; i <= showPages; i++) {  
    88.                     if (i == curpage) {  
    89.                         strBuf.append("<font class="cpagebox">" + i + "</font>");  
    90.                     } else {  
    91.                         strBuf.append(goHref(i));  
    92.                     }  
    93.                 }  
    94.                 strBuf.append("<font class="vpagebox">...</font>");  
    95.                 strBuf.append(goHref(pagecount));  
    96.             } else if (curpage > pagecount - showPages + 1) { // 右边  
    97.                 strBuf.append(goHref(1));  
    98.                 strBuf.append("<font class="vpagebox">...</font>");  
    99.                 for (int i = pagecount - showPages + 1; i <= pagecount; i++) {  
    100.                     if (i == curpage) {  
    101.                         strBuf.append("<font class="cpagebox">" + i  
    102.                                 + "</font>");  
    103.                     } else {  
    104.                         strBuf.append(goHref(i));  
    105.                     }  
    106.                 }  
    107.             } else { // 中间  
    108.                 strBuf.append(goHref(1));  
    109.                 //strBuf.append(goHref(2));  
    110.                 strBuf.append("<font class="vpagebox">...</font>");  
    111.                 int offset = (showPages - 2) / 2;  
    112.                 for (int i = curpage - offset; i <= curpage + offset; i++) {  
    113.                     if (i == curpage) {  
    114.                         strBuf.append("<font class="cpagebox">" + i + "</font>");  
    115.                     } else {  
    116.                         strBuf.append(goHref(i));  
    117.                     }  
    118.                 }  
    119.                 strBuf.append("<font class="vpagebox">...</font>");  
    120.                 strBuf.append(goHref(pagecount));  
    121.             }  
    122.         }  
    123.   
    124.         // 显示下-页  
    125.         if (curpage != pagecount) {  
    126.             // 加上链接 curpage+1  
    127.             strBuf.append("<a href="" + getHref(curpage + 1) + "" class="pagebox" >" + NEXT_PAGE + "</a>");  
    128.         }  
    129.           
    130.         strBuf.append("<input name='pageNo' type='hidden' size='3' length='3' />");  
    131.   
    132.         try {  
    133.             pageContext.getOut().println(strBuf.toString());  
    134.         } catch (IOException e) {  
    135.             e.printStackTrace();  
    136.             log.debug(e.getMessage());  
    137.         }  
    138.           
    139.         return EVAL_PAGE;  
    140.     }  
    141.   
    142.     public String getFormName() {  
    143.         return formName;  
    144.     }  
    145.   
    146.     public void setFormName(String formName) {  
    147.         this.formName = formName;  
    148.     }  
    149.   
    150.     public String getCurPage() {  
    151.         return curPage;  
    152.     }  
    153.   
    154.     public void setCurPage(String curPage) {  
    155.         this.curPage = curPage;  
    156.     }  
    157.   
    158.     public String getShowPages() {  
    159.         return showPages;  
    160.     }  
    161.   
    162.     public void setShowPages(String showPages) {  
    163.         this.showPages = showPages;  
    164.     }  
    165.   
    166.     public String getTotalPages() {  
    167.         return totalPages;  
    168.     }  
    169.   
    170.     public void setTotalPages(String totalPages) {  
    171.         this.totalPages = totalPages;  
    172.     }  
    173. }  

    说明:

    (1)如何输出到jsp页面:调用pageContext.getOut().println()。
    (2)输出后如何作处理,函数会返回几个值之一。EVAL_PAGE 表示tag已处理完毕,返回jsp页面。

    3、建立self.tld 文件

    Tld代码  收藏代码
    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>TEST Tag library</description>  
    9.   <display-name>TEST Tag</display-name>  
    10.   <tlib-version>1.0</tlib-version>  
    11.   <short-name>test</short-name>  
    12.   <uri>/test</uri>  
    13.       
    14.   <tag>  
    15.     <description>Page Info</description>  
    16.     <name>pagethird</name>  
    17.     <tag-class>com.test.web.tag.PageThirdTag</tag-class>  
    18.     <body-content>empty</body-content>  
    19.     <attribute>  
    20.         <description>the name of the current form</description>  
    21.         <name>formName</name>  
    22.         <required>true</required>  
    23.         <rtexprvalue>false</rtexprvalue>  
    24.     </attribute>  
    25.     <attribute>  
    26.         <description>Show Records</description>  
    27.         <name>showPages</name>  
    28.         <required>true</required>  
    29.         <rtexprvalue>true</rtexprvalue>  
    30.     </attribute>  
    31.     <attribute>  
    32.         <description>Current Page</description>  
    33.         <name>curPage</name>  
    34.         <required>true</required>  
    35.         <rtexprvalue>true</rtexprvalue>  
    36.     </attribute>  
    37.     <attribute>  
    38.         <description>Total Pages</description>  
    39.         <name>totalPages</name>  
    40.         <required>true</required>  
    41.         <rtexprvalue>true</rtexprvalue>  
    42.     </attribute>  
    43.   </tag>  
    44.   
    45. </taglib>  

    说明:

    short-name:taglib的名称。

    name:tag的名字。

    name:tag的名字。

    body-content:指tag之间的内容。

    required:是否必填属性。

    rtexprvalue:是否支持动态传值。

    4、web.xml中加入自定义标签定义

    Xml代码  收藏代码
    1. <jsp-config>  
    2.         <taglib>  
    3.             <taglib-uri>test</taglib-uri>     
    4.             <taglib-location>/WEB-INF/tld/self.tld</taglib-location>     
    5.         </taglib>  
    6.     </jsp-config>  

    5、jsp中使用该自定义标签

    Jsp代码  收藏代码
    1. <%@ taglib prefix="test" uri="test" %>  
    Jsp代码  收藏代码
    1. <test:pagethird formName="pictureForm" showPages="${ pageBean.showPages }" curPage="${ pageBean.pageNo }" totalPages="${ pageBean.totalPages }"/>  

     6、over

  • 相关阅读:
    Windows Server 2008 Standard Enterprise Datacenter各个版本区别
    .Net4.0并行库介绍——Cancellation Framework
    .Net4.0并行库介绍——Task
    安装VS组件提示“所选驱动不再有效。继续安装之前,请先检查安装路径的设置。”要怎么办?
    Activex 数字签名
    要使用C#实现一个ActiveX控件
    Win8系统108个运行命令 你能记住多少?
    解决 Operation must use an updateable query
    HTTP Error 404.2
    win7 系统装SQLServer2000 成功
  • 原文地址:https://www.cnblogs.com/s648667069/p/6323378.html
Copyright © 2011-2022 走看看