免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2574 | 回复: 1
打印 上一主题 下一主题

struts2的自定义分页标签 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 23:43 |只看该作者 |倒序浏览

struts2的自定义分页标签








1.写分页标签的相关支持类



Java代码
  1. 1.package com.lowca.activity.expand.struts;  
  2. 2.  
  3. 3.import javax.servlet.http.HttpServletRequest;  
  4. 4.import javax.servlet.http.HttpServletResponse;  
  5. 5.  
  6. 6.import org.apache.struts2.components.UIBean;  
  7. 7.  
  8. 8.import com.opensymphony.xwork2.util.ValueStack;  
  9. 9.  
  10. 10.public class Pagebar extends UIBean {  
  11. 11.  
  12. 12.    protected String totalRecord;  
  13. 13.    protected String totalPage;  
  14. 14.    protected String pageNo;  
  15. 15.    protected String url;  
  16. 16.    protected String style;  
  17. 17.  
  18. 18.    public String getTotalRecord() {  
  19. 19.        return totalRecord;  
  20. 20.    }  
  21. 21.  
  22. 22.    public void setTotalRecord(String totalRecord) {  
  23. 23.        this.totalRecord = totalRecord;  
  24. 24.    }  
  25. 25.  
  26. 26.    public String getTotalPage() {  
  27. 27.        return totalPage;  
  28. 28.    }  
  29. 29.  
  30. 30.    public void setTotalPage(String totalPage) {  
  31. 31.        this.totalPage = totalPage;  
  32. 32.    }  
  33. 33.  
  34. 34.    public String getPageNo() {  
  35. 35.        return pageNo;  
  36. 36.    }  
  37. 37.  
  38. 38.    public void setPageNo(String pageNo) {  
  39. 39.        this.pageNo = pageNo;  
  40. 40.    }  
  41. 41.  
  42. 42.    public String getUrl() {  
  43. 43.        return url;  
  44. 44.    }  
  45. 45.  
  46. 46.    public void setUrl(String url) {  
  47. 47.        this.url = url;  
  48. 48.    }  
  49. 49.  
  50. 50.    public String getStyle() {  
  51. 51.        return style;  
  52. 52.    }  
  53. 53.  
  54. 54.    public void setStyle(String style) {  
  55. 55.        this.style = style;  
  56. 56.    }  
  57. 57.  
  58. 58.    public Pagebar(ValueStack stack, HttpServletRequest request,  
  59. 59.            HttpServletResponse response) {  
  60. 60.        super(stack, request, response);  
  61. 61.    }  
  62. 62.  
  63. 63.    @Override  
  64. 64.    protected String getDefaultTemplate() {  
  65. 65.        return "pagebar_" + style.toLowerCase();  
  66. 66.    }  
  67. 67.  
  68. 68.    @Override  
  69. 69.    protected void evaluateExtraParams() {  
  70. 70.        super.evaluateExtraParams();  
  71. 71.        addParameter("totalRecord", findString(totalRecord));  
  72. 72.        addParameter("totalPage", findString(totalPage));  
  73. 73.        addParameter("pageNo", findString(pageNo));  
  74. 74.        addParameter("url", findString(url));  
  75. 75.        addParameter("style", style);  
  76. 76.    }  
  77. 77.  
  78. 78.}  
复制代码
Java代码
  1. 1.package com.lowca.activity.expand.struts;  
  2. 2.  
  3. 3.import javax.servlet.http.HttpServletRequest;  
  4. 4.import javax.servlet.http.HttpServletResponse;  
  5. 5.  
  6. 6.import org.apache.struts2.components.Component;  
  7. 7.import org.apache.struts2.components.UIBean;  
  8. 8.import org.apache.struts2.views.jsp.ui.AbstractUITag;  
  9. 9.  
  10. 10.import com.opensymphony.xwork2.util.ValueStack;  
  11. 11.  
  12. 12.public class PagebarTag extends AbstractUITag {  
  13. 13.  
  14. 14.    private static final long serialVersionUID = 1L;  
  15. 15.    protected String totalRecord;  
  16. 16.    protected String totalPage;  
  17. 17.    protected String pageNo;  
  18. 18.    protected String url;  
  19. 19.    protected String style = "basic";  
  20. 20.  
  21. 21.    @Override  
  22. 22.    public Component getBean(ValueStack stack, HttpServletRequest request,  
  23. 23.            HttpServletResponse response) {  
  24. 24.        return new Pagebar(stack, request, response);  
  25. 25.    }  
  26. 26.  
  27. 27.    protected void populateParams() {  
  28. 28.        super.populateParams();  
  29. 29.        Pagebar pagebar = (Pagebar) component;  
  30. 30.        pagebar.setTotalRecord(totalRecord);  
  31. 31.        pagebar.setTotalPage(totalPage);  
  32. 32.        pagebar.setPageNo(pageNo);  
  33. 33.        pagebar.setUrl(url);  
  34. 34.        pagebar.setStyle(style);  
  35. 35.    }  
  36. 36.  
  37. 37.    public String getTotalRecord() {  
  38. 38.        return totalRecord;  
  39. 39.    }  
  40. 40.  
  41. 41.    public void setTotalRecord(String totalRecord) {  
  42. 42.        this.totalRecord = totalRecord;  
  43. 43.    }  
  44. 44.  
  45. 45.    public String getTotalPage() {  
  46. 46.        return totalPage;  
  47. 47.    }  
  48. 48.  
  49. 49.    public void setTotalPage(String totalPage) {  
  50. 50.        this.totalPage = totalPage;  
  51. 51.    }  
  52. 52.  
  53. 53.    public String getPageNo() {  
  54. 54.        return pageNo;  
  55. 55.    }  
  56. 56.  
  57. 57.    public void setPageNo(String pageNo) {  
  58. 58.        this.pageNo = pageNo;  
  59. 59.    }  
  60. 60.  
  61. 61.    public String getUrl() {  
  62. 62.        return url;  
  63. 63.    }  
  64. 64.  
  65. 65.    public void setUrl(String url) {  
  66. 66.        this.url = url;  
  67. 67.    }  
  68. 68.  
  69. 69.    public String getStyle() {  
  70. 70.        return style;  
  71. 71.    }  
  72. 72.  
  73. 73.    public void setStyle(String style) {  
  74. 74.        this.style = style;  
  75. 75.    }  
  76. 76.  
  77. 77.}  
复制代码
2.写分页标签模板,模板放在src目录下d的“template/主题名”文件夹中。
因为我选择的struts2主题是simple,所以我把模板放在“template/simple”文件夹下面。
模板文件名是“pagebar_basic.ftl”.



Html代码
  1. 1.<#-- 定义顶层变量并转换 -->  
  2. 2.<#assign totalPage=parameters.totalPage?number />  
  3. 3.<#assign totalRecord=parameters.totalRecord?number />  
  4. 4.<#assign pageNo=parameters.pageNo?number />  
  5. 5.<#assign url=parameters.url />  
  6. 6.<#-- 模板逻辑 -->  
  7. 7.<#if (totalRecord>0) && (totalPage>1) && (pageNo>0) && (pageNo<=totalPage)>  
  8. 8.    <#--计算开始位置和结束位置-->  
  9. 9.    <#if (pageNo<=5)>  
  10. 10.        <#assign leftNo=1 />     
  11. 11.        <#if (totalPage<10)>  
  12. 12.            <#assign rightNo=totalPage />  
  13. 13.        <#else>  
  14. 14.            <#assign rightNo=10 />  
  15. 15.        </#if>  
  16. 16.    <#elseif (pageNo>5) && (pageNo<=totalPage-5)>  
  17. 17.        <#if (pageNo-4>0)>  
  18. 18.            <#assign leftNo=pageNo-4 />  
  19. 19.        <#else>  
  20. 20.            <#assign leftNo=1 />  
  21. 21.        </#if>  
  22. 22.        <#if (pageNo+5<=totalPage)>  
  23. 23.            <#assign rightNo=pageNo+5 />  
  24. 24.        <#else>  
  25. 25.            <#assign rightNo=totalPage />  
  26. 26.        </#if>  
  27. 27.    <#else>  
  28. 28.        <#if (totalPage-10+1>0)>  
  29. 29.            <#assign leftNo=totalPage-10+1 />  
  30. 30.        <#else>  
  31. 31.            <#assign leftNo=1 />  
  32. 32.        </#if>  
  33. 33.        <#assign rightNo=totalPage />  
  34. 34.    </#if>  
  35. 35.    <#--输出内容-->  
  36. 36.    <div class="pagebar_warpper">  
  37. 37.        <ul class="pagebar_ul">  
  38. 38.            <li>共有<span class="pagebar_rc">${totalRecord?c}</span>条数据</li>  
  39. 39.            <li class="pagebar_li">  
  40. 40.                <a href="<@getURL text=url page=1 />">首页</a>  
  41. 41.                <#if (pageNo-1>=1)>  
  42. 42.                    <a href="<@getURL text=url page=pageNo-1 />">上一页</a>  
  43. 43.                </#if>  
  44. 44.                <#list leftNo..rightNo as p>     
  45. 45.                    <#if (p==pageNo)>     
  46. 46.                        <span class="pagebar_curr">${p}</span>   
  47. 47.                    <#else>     
  48. 48.                        <a href="<@getURL text=url page=p />">${p}</a>  
  49. 49.                    </#if>  
  50. 50.                </#list>   
  51. 51.                <#if (pageNo+1<=totalPage)>  
  52. 52.                <a href="<@getURL text=url page=pageNo+1 />">下一页</a>  
  53. 53.                </#if>  
  54. 54.                <a href="<@getURL text=url page=totalPage />">尾页</a>  
  55. 55.            </li>  
  56. 56.        </ul>  
  57. 57.    </div>  
  58. 58.</#if>  
  59. 59.  
  60. 60.<#--产生动态URL的宏-->  
  61. 61.<#macro getURL text page>     
  62. 62.    <#if (text?index_of("?")>0)>     
  63. 63.        <#lt/>${text?replace("?","?pageNo="+page)}<#rt/>  
  64. 64.    <#else>     
  65. 65.        <#lt/>${text}?pageNo=${page}<#rt/>  
  66. 66.    </#if>        
  67. 67.</#macro>  
复制代码
3.在WEB-INFO目录下新建一个tld文件,名字叫lowca-struts.tld,tld文件内容如下



Xml代码
  1. 1.<?xml version="1.0" encoding="UTF-8"?>  
  2. 2.<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">  
  3. 3.<taglib>  
  4. 4.    <tlib-version>1.0</tlib-version>  
  5. 5.    <jsp-version>1.2</jsp-version>  
  6. 6.    <short-name>l</short-name>  
  7. 7.    <uri>/lowca-tags</uri>  
  8. 8.    <display-name>自定义的struts标签</display-name>  
  9. 9.      
  10. 10.    <tag>  
  11. 11.        <name>pagebar</name>  
  12. 12.        <tag-class>com.lowca.activity.expand.struts.PagebarTag</tag-class>  
  13. 13.        <body-content>jsp</body-content>  
  14. 14.        <description>分页工具条</description>  
  15. 15.        <attribute>  
  16. 16.            <name>totalRecord</name>  
  17. 17.            <required>true</required>  
  18. 18.            <rtexprvalue>true</rtexprvalue>  
  19. 19.            <type>int</type>  
  20. 20.            <description>记录数</description>  
  21. 21.        </attribute>        
  22. 22.        <attribute>  
  23. 23.            <name>totalPage</name>  
  24. 24.            <required>true</required>  
  25. 25.            <rtexprvalue>true</rtexprvalue>  
  26. 26.            <type>int</type>  
  27. 27.            <description>总页数</description>  
  28. 28.        </attribute>        
  29. 29.        <attribute>  
  30. 30.            <name>pageNo</name>  
  31. 31.            <required>true</required>  
  32. 32.            <rtexprvalue>true</rtexprvalue>  
  33. 33.            <type>int</type>  
  34. 34.            <description>页码</description>  
  35. 35.        </attribute>        
  36. 36.        <attribute>  
  37. 37.            <name>url</name>  
  38. 38.            <required>true</required>  
  39. 39.            <rtexprvalue>true</rtexprvalue>  
  40. 40.            <type>java.lang.String</type>  
  41. 41.            <description>url地址</description>  
  42. 42.        </attribute>        
  43. 43.        <attribute>  
  44. 44.            <name>style</name>  
  45. 45.            <required>false</required>  
  46. 46.            <rtexprvalue>true</rtexprvalue>  
  47. 47.            <type>java.lang.String</type>  
  48. 48.            <description>呈现样式</description>  
  49. 49.        </attribute>  
  50. 50.    </tag>  
  51. 51.</taglib>  
复制代码
4.在web.xml注册这个自定义tld



Xml代码
  1. 1.<!-- 注册自定义的标签 -->  
  2. 2.<jsp-config>  
  3. 3.    <taglib>  
  4. 4.        <taglib-uri>/lowca-tags</taglib-uri>  
  5. 5.        <taglib-location>/WEB-INF/lowca-struts.tld</taglib-location>  
  6. 6.    </taglib>  
  7. 7.</jsp-config>  
复制代码
5.在页面上面使用这个自定义标签



Html代码
  1. 1.<%@ page language="java" pageEncoding="UTF-8"%>  
  2. 2.<%@ taglib prefix="l" uri="/lowca-tags"%>  
  3. 3.  
  4. 4.<s:debug></s:debug>  
  5. 5.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  6. 6.<html>  
  7. 7.  <head>  
  8. 8.    <title>测试</title>  
  9. 9.  </head>  
  10. 10.   
  11. 11.  <body>  
  12. 12.    <l:pagebar pageNo="%{#parameters.pageNo}" url="test.jsp" totalPage="684" totalRecord="20498" style="basic" />  
  13. 13.  </body>  
  14. 14.</html>  
复制代码
至此,大功告成。

style是可以省略的属性,如果省略,则style默认值为basic。

给标签属性设置值的办法是%{ognl表达式},例如:%{#parameters.pageNo}。
pageNo当前页码,totalPage总页数,totalRecord总记录数,这些属性都必须设置成动态查询出来的,比如:%{totalPage},%{totalRecord},%{#parameters.pageNo}

你可以定义你自己的模板来扩展这个标签,模板命名规则为pagebar_${style名称}.ftl,例如:pagebar_advance.ftl。

论坛徽章:
0
2 [报告]
发表于 2011-12-21 22:01 |只看该作者
学习鸟 谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP