- 论坛徽章:
- 0
|
struts2的自定义分页标签
1.写分页标签的相关支持类
Java代码- 1.package com.lowca.activity.expand.struts;
- 2.
- 3.import javax.servlet.http.HttpServletRequest;
- 4.import javax.servlet.http.HttpServletResponse;
- 5.
- 6.import org.apache.struts2.components.UIBean;
- 7.
- 8.import com.opensymphony.xwork2.util.ValueStack;
- 9.
- 10.public class Pagebar extends UIBean {
- 11.
- 12. protected String totalRecord;
- 13. protected String totalPage;
- 14. protected String pageNo;
- 15. protected String url;
- 16. protected String style;
- 17.
- 18. public String getTotalRecord() {
- 19. return totalRecord;
- 20. }
- 21.
- 22. public void setTotalRecord(String totalRecord) {
- 23. this.totalRecord = totalRecord;
- 24. }
- 25.
- 26. public String getTotalPage() {
- 27. return totalPage;
- 28. }
- 29.
- 30. public void setTotalPage(String totalPage) {
- 31. this.totalPage = totalPage;
- 32. }
- 33.
- 34. public String getPageNo() {
- 35. return pageNo;
- 36. }
- 37.
- 38. public void setPageNo(String pageNo) {
- 39. this.pageNo = pageNo;
- 40. }
- 41.
- 42. public String getUrl() {
- 43. return url;
- 44. }
- 45.
- 46. public void setUrl(String url) {
- 47. this.url = url;
- 48. }
- 49.
- 50. public String getStyle() {
- 51. return style;
- 52. }
- 53.
- 54. public void setStyle(String style) {
- 55. this.style = style;
- 56. }
- 57.
- 58. public Pagebar(ValueStack stack, HttpServletRequest request,
- 59. HttpServletResponse response) {
- 60. super(stack, request, response);
- 61. }
- 62.
- 63. @Override
- 64. protected String getDefaultTemplate() {
- 65. return "pagebar_" + style.toLowerCase();
- 66. }
- 67.
- 68. @Override
- 69. protected void evaluateExtraParams() {
- 70. super.evaluateExtraParams();
- 71. addParameter("totalRecord", findString(totalRecord));
- 72. addParameter("totalPage", findString(totalPage));
- 73. addParameter("pageNo", findString(pageNo));
- 74. addParameter("url", findString(url));
- 75. addParameter("style", style);
- 76. }
- 77.
- 78.}
复制代码 Java代码- 1.package com.lowca.activity.expand.struts;
- 2.
- 3.import javax.servlet.http.HttpServletRequest;
- 4.import javax.servlet.http.HttpServletResponse;
- 5.
- 6.import org.apache.struts2.components.Component;
- 7.import org.apache.struts2.components.UIBean;
- 8.import org.apache.struts2.views.jsp.ui.AbstractUITag;
- 9.
- 10.import com.opensymphony.xwork2.util.ValueStack;
- 11.
- 12.public class PagebarTag extends AbstractUITag {
- 13.
- 14. private static final long serialVersionUID = 1L;
- 15. protected String totalRecord;
- 16. protected String totalPage;
- 17. protected String pageNo;
- 18. protected String url;
- 19. protected String style = "basic";
- 20.
- 21. @Override
- 22. public Component getBean(ValueStack stack, HttpServletRequest request,
- 23. HttpServletResponse response) {
- 24. return new Pagebar(stack, request, response);
- 25. }
- 26.
- 27. protected void populateParams() {
- 28. super.populateParams();
- 29. Pagebar pagebar = (Pagebar) component;
- 30. pagebar.setTotalRecord(totalRecord);
- 31. pagebar.setTotalPage(totalPage);
- 32. pagebar.setPageNo(pageNo);
- 33. pagebar.setUrl(url);
- 34. pagebar.setStyle(style);
- 35. }
- 36.
- 37. public String getTotalRecord() {
- 38. return totalRecord;
- 39. }
- 40.
- 41. public void setTotalRecord(String totalRecord) {
- 42. this.totalRecord = totalRecord;
- 43. }
- 44.
- 45. public String getTotalPage() {
- 46. return totalPage;
- 47. }
- 48.
- 49. public void setTotalPage(String totalPage) {
- 50. this.totalPage = totalPage;
- 51. }
- 52.
- 53. public String getPageNo() {
- 54. return pageNo;
- 55. }
- 56.
- 57. public void setPageNo(String pageNo) {
- 58. this.pageNo = pageNo;
- 59. }
- 60.
- 61. public String getUrl() {
- 62. return url;
- 63. }
- 64.
- 65. public void setUrl(String url) {
- 66. this.url = url;
- 67. }
- 68.
- 69. public String getStyle() {
- 70. return style;
- 71. }
- 72.
- 73. public void setStyle(String style) {
- 74. this.style = style;
- 75. }
- 76.
- 77.}
复制代码 2.写分页标签模板,模板放在src目录下d的“template/主题名”文件夹中。
因为我选择的struts2主题是simple,所以我把模板放在“template/simple”文件夹下面。
模板文件名是“pagebar_basic.ftl”.
Html代码- 1.<#-- 定义顶层变量并转换 -->
- 2.<#assign totalPage=parameters.totalPage?number />
- 3.<#assign totalRecord=parameters.totalRecord?number />
- 4.<#assign pageNo=parameters.pageNo?number />
- 5.<#assign url=parameters.url />
- 6.<#-- 模板逻辑 -->
- 7.<#if (totalRecord>0) && (totalPage>1) && (pageNo>0) && (pageNo<=totalPage)>
- 8. <#--计算开始位置和结束位置-->
- 9. <#if (pageNo<=5)>
- 10. <#assign leftNo=1 />
- 11. <#if (totalPage<10)>
- 12. <#assign rightNo=totalPage />
- 13. <#else>
- 14. <#assign rightNo=10 />
- 15. </#if>
- 16. <#elseif (pageNo>5) && (pageNo<=totalPage-5)>
- 17. <#if (pageNo-4>0)>
- 18. <#assign leftNo=pageNo-4 />
- 19. <#else>
- 20. <#assign leftNo=1 />
- 21. </#if>
- 22. <#if (pageNo+5<=totalPage)>
- 23. <#assign rightNo=pageNo+5 />
- 24. <#else>
- 25. <#assign rightNo=totalPage />
- 26. </#if>
- 27. <#else>
- 28. <#if (totalPage-10+1>0)>
- 29. <#assign leftNo=totalPage-10+1 />
- 30. <#else>
- 31. <#assign leftNo=1 />
- 32. </#if>
- 33. <#assign rightNo=totalPage />
- 34. </#if>
- 35. <#--输出内容-->
- 36. <div class="pagebar_warpper">
- 37. <ul class="pagebar_ul">
- 38. <li>共有<span class="pagebar_rc">${totalRecord?c}</span>条数据</li>
- 39. <li class="pagebar_li">
- 40. <a href="<@getURL text=url page=1 />">首页</a>
- 41. <#if (pageNo-1>=1)>
- 42. <a href="<@getURL text=url page=pageNo-1 />">上一页</a>
- 43. </#if>
- 44. <#list leftNo..rightNo as p>
- 45. <#if (p==pageNo)>
- 46. <span class="pagebar_curr">${p}</span>
- 47. <#else>
- 48. <a href="<@getURL text=url page=p />">${p}</a>
- 49. </#if>
- 50. </#list>
- 51. <#if (pageNo+1<=totalPage)>
- 52. <a href="<@getURL text=url page=pageNo+1 />">下一页</a>
- 53. </#if>
- 54. <a href="<@getURL text=url page=totalPage />">尾页</a>
- 55. </li>
- 56. </ul>
- 57. </div>
- 58.</#if>
- 59.
- 60.<#--产生动态URL的宏-->
- 61.<#macro getURL text page>
- 62. <#if (text?index_of("?")>0)>
- 63. <#lt/>${text?replace("?","?pageNo="+page)}<#rt/>
- 64. <#else>
- 65. <#lt/>${text}?pageNo=${page}<#rt/>
- 66. </#if>
- 67.</#macro>
复制代码 3.在WEB-INFO目录下新建一个tld文件,名字叫lowca-struts.tld,tld文件内容如下
Xml代码- 1.<?xml version="1.0" encoding="UTF-8"?>
- 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.<taglib>
- 4. <tlib-version>1.0</tlib-version>
- 5. <jsp-version>1.2</jsp-version>
- 6. <short-name>l</short-name>
- 7. <uri>/lowca-tags</uri>
- 8. <display-name>自定义的struts标签</display-name>
- 9.
- 10. <tag>
- 11. <name>pagebar</name>
- 12. <tag-class>com.lowca.activity.expand.struts.PagebarTag</tag-class>
- 13. <body-content>jsp</body-content>
- 14. <description>分页工具条</description>
- 15. <attribute>
- 16. <name>totalRecord</name>
- 17. <required>true</required>
- 18. <rtexprvalue>true</rtexprvalue>
- 19. <type>int</type>
- 20. <description>记录数</description>
- 21. </attribute>
- 22. <attribute>
- 23. <name>totalPage</name>
- 24. <required>true</required>
- 25. <rtexprvalue>true</rtexprvalue>
- 26. <type>int</type>
- 27. <description>总页数</description>
- 28. </attribute>
- 29. <attribute>
- 30. <name>pageNo</name>
- 31. <required>true</required>
- 32. <rtexprvalue>true</rtexprvalue>
- 33. <type>int</type>
- 34. <description>页码</description>
- 35. </attribute>
- 36. <attribute>
- 37. <name>url</name>
- 38. <required>true</required>
- 39. <rtexprvalue>true</rtexprvalue>
- 40. <type>java.lang.String</type>
- 41. <description>url地址</description>
- 42. </attribute>
- 43. <attribute>
- 44. <name>style</name>
- 45. <required>false</required>
- 46. <rtexprvalue>true</rtexprvalue>
- 47. <type>java.lang.String</type>
- 48. <description>呈现样式</description>
- 49. </attribute>
- 50. </tag>
- 51.</taglib>
复制代码 4.在web.xml注册这个自定义tld
Xml代码- 1.<!-- 注册自定义的标签 -->
- 2.<jsp-config>
- 3. <taglib>
- 4. <taglib-uri>/lowca-tags</taglib-uri>
- 5. <taglib-location>/WEB-INF/lowca-struts.tld</taglib-location>
- 6. </taglib>
- 7.</jsp-config>
复制代码 5.在页面上面使用这个自定义标签
Html代码- 1.<%@ page language="java" pageEncoding="UTF-8"%>
- 2.<%@ taglib prefix="l" uri="/lowca-tags"%>
- 3.
- 4.<s:debug></s:debug>
- 5.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- 6.<html>
- 7. <head>
- 8. <title>测试</title>
- 9. </head>
- 10.
- 11. <body>
- 12. <l:pagebar pageNo="%{#parameters.pageNo}" url="test.jsp" totalPage="684" totalRecord="20498" style="basic" />
- 13. </body>
- 14.</html>
复制代码 至此,大功告成。
style是可以省略的属性,如果省略,则style默认值为basic。
给标签属性设置值的办法是%{ognl表达式},例如:%{#parameters.pageNo}。
pageNo当前页码,totalPage总页数,totalRecord总记录数,这些属性都必须设置成动态查询出来的,比如:%{totalPage},%{totalRecord},%{#parameters.pageNo}
你可以定义你自己的模板来扩展这个标签,模板命名规则为pagebar_${style名称}.ftl,例如:pagebar_advance.ftl。 |
|