`
燮羽天翔
  • 浏览: 109821 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自己写struts2分页标签

阅读更多

想自己做标签的朋友可以来看一下.

这几天一直在研究Web分页,可能有的朋友会觉得这很简单,我想说的是,把任何简单的事做好就是不简单.我也正在努力着.

因为在用struts2写一个东西,分页用得比较频繁,所以决定自己写一个分页标签,编入自己的类库,准备一用它很多年,拷贝好带身边:)

这里把源码跟大家分享,希望对某些朋友有用.其实我一直觉得标签和.net里服务器控件是一回事.struts2的高扩展性为我们自己实现标签提供了很大的便捷.OK,进入主题

实现自定义struts2标签分三个部分:

1.定义标签 在这步要创建标签说明文件 *.tld

2.实现标签组件 这里只需继承struts2中的组件类和标签类就行了

3.用freemarker渲染标签 这步要创建一个*.ftl文件.同时我把相关分页逻辑也写在了这里

这个标签是用在struts2环境里的,所以需要你的项目需要引入struts2的常用包.这里就不多说了.

下面是效果图效果图

 

 

 

(没有mkdir权限...弄张图不容易...)

下面是源代码:

在/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>   

 

感觉freemarker用起来挺方便的,我花了一个上午粗略地学了一下.有兴趣可以看这篇笔记:FreeMarker详解:http://student.csdn.net/space.php?uid=46942&do=blog&id=4769

最后,需要在你的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就行了

其中directJump属性所指的值还没有实现,希望有兴趣的朋友能一起完善它.

 

转载自:http://student.csdn.net/space.php?uid=46942&do=blog&id=4878

分享到:
评论
1 楼 osacar 2010-08-28  

弄个分页这么麻烦的啊??

相关推荐

    Struts2自定义分页标签

    Struts2自定义分页标签很详细了,新手可以少走弯路,节约时间 资源包括了后台如何操作。

    基于struts2 自定义标签 分页

    基于struts2的自定义分页标签实现的一个demo,连接数据库为Oracle

    自定义struts2 分页标签

    自定义struts2 分页标签,值得学习..

    struts2分页代码的示例

    在开发web程序时,从数据库中取出来的数据一般都要分页显示在页面上,分页显示的方法非常多,有人写自定义标签,用自己写的标签进行分页显示,我个人觉得这是一种比较好的方法。下面是我用Struts2做的一个分页显示...

    java分页。struts2标签

    struts2标签java分页。struts2标签java分页。struts2标签java分页。struts2标签java分页。struts2标签java分页。struts2标签java分页。struts2标签java分页。struts2标签java分页。struts2标签java分页。struts2标签...

    struts2实现分页

    struts2中&lt;s:bean name="org.apache.struts2.util.Counter" id="counter"&gt;循环标签的使用

    struts分页标签

    struts分页标签struts分页标签struts分页标签struts分页标签

    Struts2分页-自定义标签-类似百度分页

    NULL 博文链接:https://wxb-j2ee.iteye.com/blog/916350

    Struts+Hibernate分页标签

    Struts+Hibernate分页标签 Struts+Hibernate分页标签

    struts2做的分页标签

    看到网上代码,写的比较乱,自己整 理了一下,写了一个项目,用myeclipse6.6直接导入即可

    Struts 2 自定义标签分页

    Struts 2 自定义标签分页,有简单上下分页和数字分页

    struts2自定义标签案例详解

    下面以一个自定义的分页标签为例,进行说明。 其实,开发自定义标签并不需要Struts2的支持,一般情况下,只需要继承javax.servlet.jsp.tagext.BodyTagSupport类,重写doStartTag,doEndTag等方法即可。这里在实现...

    struts2+Jquery分页插件

    以struts2标签的形式提供分页功能,使用简便,杜绝手工去拼分页标签的样式。目前提供了两种方式,直接原生的jquery调用,使用seajs模块化的方式。 源码地址:http://code.google.com/p/paginationex/source/checkout

    struts标签分页(实用)

    struts标签分页(实用) 带源码,更详细说明可见http://hi.baidu.com/zhuoyibao

    struts2 完美实现 标签分页

    在 Myeclipse6.5 SQLServer2005 上成功测试通过

    Struts2实现分页代码

    在开发web程序时,从数据库中取出来的数据一般都要分页显示在页面上,分页显示的方法非常多,有人写自定义标签,用自己写的标签进行分页显示,我个人觉得这是一种比较好的方法。下面是我用Struts2做的一个分页显示...

    自定义标签代替Struts标签实现分页

    自定义标签实现分页,分页信息都封装在一个继承自TagSupport的java文件中,自定义一个tld文件,jsp页面通过taglib指令引入tld文件,使用自定义标签。

    Struts带分页有查询

    这是一个struts写的有分页,分页是用标签写的用起来非常方便, 可以将查询在分页中

Global site tag (gtag.js) - Google Analytics