package com.lowca.activity.expand.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.components.UIBean; import com.opensymphony.xwork2.util.ValueStack; public class Pagebar extends UIBean { protected String totalRecord; protected String totalPage; protected String pageNo; protected String url; protected String style; public String getTotalRecord() { return totalRecord; } public void setTotalRecord(String totalRecord) { this.totalRecord = totalRecord; } public String getTotalPage() { return totalPage; } public void setTotalPage(String totalPage) { this.totalPage = totalPage; } public String getPageNo() { return pageNo; } public void setPageNo(String pageNo) { this.pageNo = pageNo; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public Pagebar(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { super(stack, request, response); } @Override protected String getDefaultTemplate() { return "pagebar_" + style.toLowerCase(); } @Override protected void evaluateExtraParams() { super.evaluateExtraParams(); addParameter("totalRecord", findString(totalRecord)); addParameter("totalPage", findString(totalPage)); addParameter("pageNo", findString(pageNo)); addParameter("url", findString(url)); addParameter("style", style); } }
package com.lowca.activity.expand.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.components.Component; import org.apache.struts2.components.UIBean; import org.apache.struts2.views.jsp.ui.AbstractUITag; import com.opensymphony.xwork2.util.ValueStack; public class PagebarTag extends AbstractUITag { private static final long serialVersionUID = 1L; protected String totalRecord; protected String totalPage; protected String pageNo; protected String url; protected String style = "basic"; @Override public Component getBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { return new Pagebar(stack, request, response); } protected void populateParams() { super.populateParams(); Pagebar pagebar = (Pagebar) component; pagebar.setTotalRecord(totalRecord); pagebar.setTotalPage(totalPage); pagebar.setPageNo(pageNo); pagebar.setUrl(url); pagebar.setStyle(style); } public String getTotalRecord() { return totalRecord; } public void setTotalRecord(String totalRecord) { this.totalRecord = totalRecord; } public String getTotalPage() { return totalPage; } public void setTotalPage(String totalPage) { this.totalPage = totalPage; } public String getPageNo() { return pageNo; } public void setPageNo(String pageNo) { this.pageNo = pageNo; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } }
2.写分页标签模板,模板放在src目录下d的“template/主题名”文件夹中。
因为我选择的struts2主题是simple,所以我把模板放在“template/simple”文件夹下面。
模板文件名是“pagebar_basic.ftl”.
<#-- 定义顶层变量并转换 --> <#assign totalPage=parameters.totalPage?number /> <#assign totalRecord=parameters.totalRecord?number /> <#assign pageNo=parameters.pageNo?number /> <#assign url=parameters.url /> <#-- 模板逻辑 --> <#if (totalRecord>0) && (totalPage>1) && (pageNo>0) && (pageNo<=totalPage)> <#--计算开始位置和结束位置--> <#if (pageNo<=5)> <#assign leftNo=1 /> <#if (totalPage<10)> <#assign rightNo=totalPage /> <#else> <#assign rightNo=10 /> </#if> <#elseif (pageNo>5) && (pageNo<=totalPage-5)> <#if (pageNo-4>0)> <#assign leftNo=pageNo-4 /> <#else> <#assign leftNo=1 /> </#if> <#if (pageNo+5<=totalPage)> <#assign rightNo=pageNo+5 /> <#else> <#assign rightNo=totalPage /> </#if> <#else> <#if (totalPage-10+1>0)> <#assign leftNo=totalPage-10+1 /> <#else> <#assign leftNo=1 /> </#if> <#assign rightNo=totalPage /> </#if> <#--输出内容--> <div class="pagebar_warpper"> <ul class="pagebar_ul"> <li>共有<span class="pagebar_rc">${totalRecord?c}</span>条数据</li> <li class="pagebar_li"> <a href="<@getURL text=url page=1 />">首页</a> <#if (pageNo-1>=1)> <a href="<@getURL text=url page=pageNo-1 />">上一页</a> </#if> <#list leftNo..rightNo as p> <#if (p==pageNo)> <span class="pagebar_curr">${p}</span> <#else> <a href="<@getURL text=url page=p />">${p}</a> </#if> </#list> <#if (pageNo+1<=totalPage)> <a href="<@getURL text=url page=pageNo+1 />">下一页</a> </#if> <a href="<@getURL text=url page=totalPage />">尾页</a> </li> </ul> </div> </#if> <#--产生动态URL的宏--> <#macro getURL text page> <#if (text?index_of("?")>0)> <#lt/>${text?replace("?","?pageNo="+page)}<#rt/> <#else> <#lt/>${text}?pageNo=${page}<#rt/> </#if> </#macro>
3.在WEB-INFO目录下新建一个tld文件,名字叫lowca-struts.tld,tld文件内容如下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>l</short-name> <uri>/lowca-tags</uri> <display-name>自定义的struts标签</display-name> <tag> <name>pagebar</name> <tag-class>com.lowca.activity.expand.struts.PagebarTag</tag-class> <body-content>jsp</body-content> <description>分页工具条</description> <attribute> <name>totalRecord</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>int</type> <description>记录数</description> </attribute> <attribute> <name>totalPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>int</type> <description>总页数</description> </attribute> <attribute> <name>pageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>int</type> <description>页码</description> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> <description>url地址</description> </attribute> <attribute> <name>style</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> <description>呈现样式</description> </attribute> </tag> </taglib>
4.在web.xml注册这个自定义tld
<!-- 注册自定义的标签 --> <jsp-config> <taglib> <taglib-uri>/lowca-tags</taglib-uri> <taglib-location>/WEB-INF/lowca-struts.tld</taglib-location> </taglib> </jsp-config>
5.在页面上面使用这个自定义标签
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="l" uri="/lowca-tags"%> <s:debug></s:debug> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>测试</title> </head> <body> <l:pagebar pageNo="%{#parameters.pageNo}" url="test.jsp" totalPage="684" totalRecord="20498" style="basic" /> </body> </html>
至此,大功告成。
style是可以省略的属性,如果省略,则style默认值为basic。
给标签属性设置值的办法是%{ognl表达式},例如:%{#parameters.pageNo}。
pageNo当前页码,totalPage总页数,totalRecord总记录数,这些属性都必须设置成动态查询出来的,比如:%{totalPage},%{totalRecord},%{#parameters.pageNo}
你可以定义你自己的模板来扩展这个标签,模板命名规则为pagebar_${style名称}.ftl,例如:pagebar_advance.ftl。