实现自定义struts2标签分三个部分:
1.定义标签 在这步要创建标签说明文件 *.tld
2.实现标签组件 这里只需继承struts2中的组件类和标签类就行了
3.用freemarker渲染标签 这步要创建一个*.ftl文件.同时我把相关分页逻辑也写在了这里
下面是效果图
下面是源代码:
在/web-inf下建立tlds文件夹,在其中新建iThink.tld文件(名字随个人喜好),内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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>2.2.3</tlib-version> <jsp-version>1.2</jsp-version> <short-name>ithink</short-name> <uri>/ithink-tags</uri> <display-name>"iThink Tags"</display-name> <description><![CDATA["iThink is a student team. iThink Tags supports custom components that usually used in web develop"]]></description> <tag> <name>pager</name> <tag-class>org.icim.pager.struts2.PagerTag</tag-class> <body-content>JSP</body-content> <attribute> <name>totalRecord</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[总记录数]]></description> </attribute> <attribute> <name>totalPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[总页数]]></description> </attribute> <attribute> <name>curPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[当前页号]]></description> </attribute> <attribute> <name>pageLimit</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[每版最多显示的页数,最好为奇数]]></description> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[翻页时的请求地址,如:list?page={page},其中的{page}会被动态地替换]]></description> </attribute> <attribute> <name>curCssClass</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[当前页的span标签的cssClass]]></description> </attribute> <attribute> <name>showTotalPage</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[是否显示总页数,默认为"true"]]></description> </attribute> <attribute> <name>showTotalRecord</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[是否显示总记录数,只有设置了总记录数属性的时候才有效,默认为"false"]]></description> </attribute> <attribute> <name>directJumpType</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[直接跳转的方式,"goto"或"select",默认为none]]></description> </attribute> <!--以下是原UIBean通用的属性--> <attribute> <name>accesskey</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html accesskey attribute on rendered html element]]></description> </attribute> <attribute> <name>cssClass</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[The css class to use for element]]></description> </attribute> <attribute> <name>cssStyle</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[The css style definitions for element to use]]></description> </attribute> <attribute> <name>disabled</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html disabled attribute on rendered html element]]></description> </attribute> <attribute> <name>id</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[id for referencing element. For UI and form tags it will be used as HTML id attribute]]></description> </attribute> <attribute> <name>key</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the key (name, value, label) for this particular component]]></description> </attribute> <attribute> <name>label</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Label expression used for rendering a element specific label]]></description> </attribute> <attribute> <name>labelposition</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Define label position of form element (top/left)]]></description> </attribute> <attribute> <name>name</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[The name to set for element]]></description> </attribute> <attribute> <name>onblur</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[ Set the html onblur attribute on rendered html element]]></description> </attribute> <attribute> <name>onchange</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onchange attribute on rendered html element]]></description> </attribute> <attribute> <name>onclick</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onclick attribute on rendered html element]]></description> </attribute> <attribute> <name>ondblclick</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html ondblclick attribute on rendered html element]]></description> </attribute> <attribute> <name>onfocus</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onfocus attribute on rendered html element]]></description> </attribute> <attribute> <name>onkeydown</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onkeydown attribute on rendered html element]]></description> </attribute> <attribute> <name>onkeypress</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onkeypress attribute on rendered html element]]></description> </attribute> <attribute> <name>onkeyup</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onkeyup attribute on rendered html element]]></description> </attribute> <attribute> <name>onmousedown</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onmousedown attribute on rendered html element]]></description> </attribute> <attribute> <name>onmousemove</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onmousemove attribute on rendered html element]]></description> </attribute> <attribute> <name>onmouseout</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onmouseout attribute on rendered html element]]></description> </attribute> <attribute> <name>onmouseover</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onmouseover attribute on rendered html element]]></description> </attribute> <attribute> <name>onmouseup</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onmouseup attribute on rendered html element]]></description> </attribute> <attribute> <name>onselect</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html onselect attribute on rendered html element]]></description> </attribute> <attribute> <name>required</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[If set to true, the rendered element will indicate that input is required]]></description> </attribute> <attribute> <name>requiredposition</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Define required position of required form element (left|right)]]></description> </attribute> <attribute> <name>tabindex</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html tabindex attribute on rendered html element]]></description> </attribute> <attribute> <name>template</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[The template (other than default) to use for rendering the element]]></description> </attribute> <attribute> <name>templateDir</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[The template directory.]]></description> </attribute> <attribute> <name>theme</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[The theme (other than default) to use for rendering the element]]></description> </attribute> <attribute> <name>title</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the html title attribute on rendered html element]]></description> </attribute> <attribute> <name>tooltip</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the tooltip of this particular component]]></description> </attribute> <attribute> <name>tooltipConfig</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Set the tooltip configuration]]></description> </attribute> <attribute> <name>value</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Preset the value of input element.]]></description> </attribute> </tag> <tag> <name>textfield</name> <tag-class>org.apache.struts2.views.jsp.ui.TextFieldTag</tag-class> <body-content>JSP</body-content> <description><![CDATA[Render an HTML input field of type text]]></description> </tag> </taglib>
原UIBean通用的属性我是直接从struts2源代码包里面copy来的.支持属性非常丰富.呵呵
建立组件类和标签类:
Pager.java:
/*********************************************************************** * * Pager.java * * @creator Bruce Tsai * @create-time Jun 12, 2009 10:53:38 PM ***********************************************************************/ package org.icim.pager.struts2; import org.apache.struts2.components.UIBean; import org.apache.struts2.views.annotations.StrutsTag; import org.apache.struts2.views.annotations.StrutsTagAttribute; import com.opensymphony.xwork2.util.ValueStack; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * struts2版的分页标签 * */ @StrutsTag(name = "pager", tldTagClass = "org.icim.pager.struts2.PagerTag", description = "struts2 pager by ithink") public class Pager extends UIBean { final public static String TEMPLATE = "pager"; protected String totalRecord; protected String totalPage; protected String curPage; protected String pageLimit; protected String url; protected String curCssClass; protected String showTotalPage; protected String showTotalRecord; protected String directJumpType; public Pager(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { super(stack, request, response); } /** * 用于返回模板的名字,Struts2会自动在后面加入.ftl扩展名以找到特定的模板文件。 */ @Override protected String getDefaultTemplate() { return TEMPLATE; } /** * 设置UIBean的属性,一般Tag中有几个这样的属性,这里就有几个 StrutsTagAttribute注解,说明该属性是int类型,这一步很重要 * * @param totalPage */ @StrutsTagAttribute(description = "total records", type = "Long") public void setTotalRecord(String totalRecord) { this.totalRecord = totalRecord; } @StrutsTagAttribute(description = "total pages", type = "Integer") public void setTotalPage(String totalPage) { this.totalPage = totalPage; } @StrutsTagAttribute(description = "current page", type = "Integer") public void setCurPage(String curPage) { this.curPage = curPage; } @StrutsTagAttribute(description = "how many pages in a panel once", type = "Integer") public void setPageLimit(String pageLimit) { this.pageLimit = pageLimit; } @StrutsTagAttribute(description = "url to be linked", type = "String") public void setUrl(String url) { this.url = url; } @StrutsTagAttribute(description = "css style of current page", type = "String") public void setCurCssClass(String curCssClass) { this.curCssClass = curCssClass; } @StrutsTagAttribute(description = "whether to show totalPage", type = "Boolean", defaultValue = "true") public void setShowTotalPage(String showTotalPage) { this.showTotalPage = showTotalPage; } @StrutsTagAttribute(description = "whether to show currentPage", type = "Boolean", defaultValue = "false") public void setShowTotalRecord(String showTotalRecord) { this.showTotalRecord = showTotalRecord; } // TODO 直接页面跳转 // 这里的directJumpType默认值为none, 可选值为 'select', 'goto' @StrutsTagAttribute(description = "show type of direct jump type. such as select,textbox which can lead going to a page directly", type = "String", defaultValue = "none") public void setDirectJumpType(String directJumpType) { this.directJumpType = directJumpType; } /** * 重写evaluateExtraParams()方法,在UIBean初始化后会调用这个方法来初始化设定参数,如addParameter方法,会在freemarker里的parameters里加入一个key * value。这里要注意findString,还有相关的findxxxx方法,它们是已经封装好了的解释ognl语法的工具,具体是怎么样的,大家可以查看一下UIBean的api * doc */ @Override protected void evaluateExtraParams() { super.evaluateExtraParams(); // findValue()方法本身已对OGNL进行了处理 if (totalRecord != null) { addParameter("totalRecord", findValue(totalRecord)); } if (totalPage != null) { addParameter("totalPage", findValue(totalPage)); } if (curPage != null) { addParameter("curPage", findValue(curPage)); } if (pageLimit != null) { addParameter("pageLimit", findValue(pageLimit)); } if (url != null) { addParameter("url", findValue(url, String.class)); } if (curCssClass != null) { addParameter("curCssClass", findValue(curCssClass,String.class)); } if (showTotalPage != null) { addParameter("showTotalPage", findValue(showTotalPage, Boolean.class)); } if (showTotalRecord != null) { addParameter("showTotalRecord", findValue(showTotalRecord,Boolean.class)); } if (directJumpType != null) { addParameter("directJumpType", findValue(directJumpType)); } } }
PagerTag.java:
/*********************************************************************** * * PagerTag.java * * @creator Bruce Tsai * @create-time Jun 13, 2009 12:08:53 AM ***********************************************************************/ package org.icim.pager.struts2; import org.apache.struts2.views.jsp.ui.AbstractUITag; import org.apache.struts2.components.Component; import com.opensymphony.xwork2.util.ValueStack; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * struts2 版分页标签 */ public class PagerTag extends AbstractUITag { private static final long serialVersionUID = 719669934024053141L; protected String totalRecord; protected String totalPage; protected String curPage; protected String pageLimit; protected String url; protected String curCssClass; protected String showTotalPage; protected String showTotalRecord; protected String directJumpType; protected void populateParams() { super.populateParams(); Pager pager = (Pager) component; pager.setTotalRecord(totalRecord); pager.setTotalPage(totalPage); pager.setCurPage(curPage); pager.setPageLimit(pageLimit); pager.setUrl(url); pager.setCurCssClass(curCssClass); pager.setShowTotalPage(showTotalPage); pager.setShowTotalRecord(showTotalRecord); pager.setDirectJumpType(directJumpType); } @Override public Component getBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { return new Pager(stack, request, response); } public void setTotalRecord(String totalRecord){ this.totalRecord = totalRecord; } public void setTotalPage(String totalPage) { this.totalPage = totalPage; } public void setCurPage(String curPage) { this.curPage = curPage; } public void setPageLimit(String pageLimit) { this.pageLimit = pageLimit; } public void setUrl(String url) { this.url = url; } public void setCurCssClass(String curCssClass) { this.curCssClass = curCssClass; } public void setShowTotalPage(String showTotalPage) { this.showTotalPage = showTotalPage; } public void setShowTotalRecord(String showTotalRecord) { this.showTotalRecord = showTotalRecord; } public void setDirectJumpType(String directJumpType) { this.directJumpType = directJumpType; } }
在类文件根目录下(在myeclipse中是在src目录下,发布后会copy到web-inf/classes/ 里)建立template/simple/ 目录,里面创建pager.ftl文件,内容如下:
<#--显示总记录数,只有同时设置了总记录数和显示总记录数时才有效--> <#if (parameters.totalRecord?exists) && (parameters.showTotalRecord?exists) && (parameters.showTotalRecord == true)> <span>${parameters.totalRecord}</span><#rt/> </#if> <#--打印选择页的面板 --> <#--首先,当parameters.pageLimit为空时将其设定为total--> <#if (parameters.pageLimit?exists)> <#assign limit = parameters.pageLimit /> <#else> <#assign limit = parameters.totalPage /> </#if> <@pagePanel cur=parameters.curPage total=parameters.totalPage url=parameters.url limitlimit=limit /> <#--打印选择页的面板结束--> <#--显示总页数--> <#if parameters.showTotalPage?exists && (parameters.showTotalPage == true)> <span>..${parameters.totalPage}</span><#rt/> </#if> <#--打印翻页面板的宏,配合printPage,printButton--> <#macro pagePanel cur total limit url curCssClass = "cur_page"><#--curCssClass默认值为cur_page--> <#--limit的中间数--> <#assign l_mid = (limit/2)?int + 1 /> <#--total的中间数--> <#assign t_mid = (total/2)?int /> <#--情况一:总页数小于等于限制显示页数,这时显示所有页--> <#if total <= limit> <@printButton direct="left" valuable = false /> <@printPage left = 1 right = total curcur = cur urlurl = url curCssClasscurCssClass = curCssClass /> <@printButton direct="right" valuable = false /> <#else> <#--情况二:总页数大于限制显示页数,这时又分三种情况--> <#--情况1:显示的limit个页号在total个页面中偏向左端,1作为最左边的页号,当前页没能显示在中间,偏左,例: total = 20,cur = 2,limit = 5.显示的页面为:1 [2] 3 4 5 这种情况 cur <= l_mid --> <#if cur <= l_mid> <@printButton direct="left" valuable = false /> <@printPage left = 1 right = limit curcur = cur urlurl = url curCssClasscurCssClass = curCssClass /> <@printButton direct="right" valuable = true /> <#--情况2:显示的limit个页号在total个页面中偏向右端,total作为最右边的页号,当前页没能显示在中间,偏右,例: total = 20,cur = 19,limit = 5.显示的页面为:16 17 18 [19] 20 这种情况 cur > total - l_mid --> <#elseif (cur > (total - l_mid))> <@printButton direct="left" valuable = true /> <@printPage left = (total - limit + 1) right = total curcur = cur urlurl = url curCssClasscurCssClass = curCssClass /> <@printButton direct="right" valuable = false /> <#--在中间的情况--> <#else> <@printButton direct="left" valuable = true /> <@printPage left = (cur - l_mid + 1) right = (cur + l_mid -1) curcur = cur urlurl = url curCssClasscurCssClass = curCssClass /> <@printButton direct="right" valuable = true /> </#if> </#if> </#macro> <#--根据最左与最右的页号来打印所显示的页面,当前页为的cssStyle为curCssClass--> <#macro printPage left right cur url curCssClass> <#list left..right as p> <#if p == cur> <span class = "${curCssClass}" >${p}</span><#rt/> <#else> <a href = "<@makeURL text=url page=p />">${p}</a><#rt/> </#if> </#list> </#macro> <#--翻页控制按钮,direct选择"left"或"right",avaluable设置是否有链接--> <#macro printButton direct valuable = true> <#if direct == "left"> <#if valuable> <a href="<@makeURL text=parameters.url page=1 />"> << </a><#rt/> <a href="<@makeURL text=parameters.url page=parameters.curPage-1 />"> < </a><#rt/> <#else> <span><<</span><#rt/> <span><</span><#rt/> </#if> <#else> <#if valuable> <a href="<@makeURL text=parameters.url page=parameters.curPage+1 />"> > </a><#rt/> <a href="<@makeURL text=parameters.url page=parameters.totalPage />"> >> </a><#rt/> <#else> <span>></span><#rt/> <span>>></span><#rt/> </#if> </#if> </#macro> <#--产生动态URL的宏--> <#macro makeURL text page> <#if text?last_index_of("{page}") < 0> ${text}?page=${page} <#else> ${text?replace("{page}",page)} </#if> </#macro>
最后,需要在你的struts.xml配置文件里做如下设置
<constant name="struts.ui.theme" value="simple" />
也就是对应之前建立的theme/simple目录
这样标签部分就做完了.下面使用标签.建立test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ taglib prefix="ithink" uri="/ithink-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>pager tag test</title> </head> <body> <div class="pagination"> <ithink:pager curPage="3" totalPage="88" url="listPaper.action?pno={page}" pageLimit="9" showTotalPage="true" totalRecord="1988" showTotalRecord="true" /> </div> </body> </html>
还需要为它设置CSS样式:
/*---------------------pagination-----------------------------*/ div.pagination { padding:5px; margin:5px; text-align:center; float:left; font-size:12px; } div.pagination a { padding: 2px 5px 2px 5px; margin-right: 2px; border: 1px solid #ddd; text-decoration: none; color: #d8325d; } div.pagination a:hover, div.pagination a:active { border:1px solid #ddd; color: #fff; background-color: #d8325d; } div.pagination span.cur_page { padding: 2px 5px 2px 5px; margin-right: 2px; border: 1px solid #ddd; font-weight: bold; background-color: #d8325d; color: #FFF; } div.pagination span { padding: 2px 5px 2px 5px; margin-right: 2px; border: 1px solid #ddd; text-decoration: none; color: #d8325d; }
到此一切都OVER了.如果你觉得我的实现不能满足要求,完全可以自己再写一个pager.ftl.只要在使用标签时指定theme属性,比如说"text",然后再在theme/text目录下重新写一个pager.ftl就行了