?在某個項目中,經過幾個同事的修改, 最終得到這個支持分頁的action基類, 使用非常簡單:
1. 在寫action類時把派生ActionSupport類改成派生這個PaginationSupportAction類
2. 實現這兩個抽象方法, 其中doExecute方法就是你原來的execute方法的代碼, queryCount()返回符合條件的記錄數


????/**?*//**
?????*?just?like?interface?Action?:?public?String?execute()
?????*?
?????*?@throws?Exception
?????*?@return?String?the?same?as?execute()?interface
?????*/
????protected?abstract?String?doExecute()
????????????throws?Exception;


????/**?*//**
?????*?query?the?item?count
?????*?
?????*?@return?int
?????*/
????protected?abstract?int?queryCount()
????????????throws?Exception;3. 在jsp顯示記錄的下面添加:

<%
@?include?file="../common/pagination.jsp"%>下面是這兩個文件的源代碼:
PaginationSupportAction.java:
package?com.kelefa.common.action;

import?java.util.Enumeration;
import?java.util.HashMap;
import?java.util.LinkedList;
import?java.util.List;
import?java.util.Map;
import?javax.servlet.http.HttpServletRequest;

import?org.apache.log4j.Logger;
import?com.opensymphony.webwork.ServletActionContext;
import?com.opensymphony.xwork.ActionSupport;


/**?*//**
?*?<pre>
?*?????分頁action的基類,需要分頁的action可繼承這個類,并實現以下2個抽象方法
?*?????????protected?abstract?String?doExecute()?throws?Exception?;
?*?????????protected?abstract?int?queryCount();
?*?</pre>
?*?
?*?<p>
?*?Copyright:?Copyright?(c)?2005
?*?</p>
?*?
?*?@author?楊杰榮
?*?@version?1.0
?*/
public?abstract?class?PaginationSupportAction
????????extends?ActionSupport


{
????private?static?final?Logger?log?=?Logger
????????????.getLogger(?PaginationSupportAction.class?);

????protected?String?pageUrl;

????protected?int?pageList?=?15;?//?從action.xml傳遞過來的每頁記錄數

????protected?int?pageNo?=?1;

????protected?int?itemCount;

????protected?int?pageCount;

????protected?String?preUrl;

????protected?String?nextUrl;

????protected?String?firstUrl;

????protected?String?lastUrl;

????protected?List?pageJumpParameters;

????public?PaginationSupportAction()

????
{
????}

????private?void?getURL()

????
{
????????//?if?(?pageUrl?!=?null?)?//?在action-*****.xml里設置了
????????//?pageUrl?=?null;

????????HttpServletRequest?request?=?ServletActionContext.getRequest();
????????String?servletPath?=?(String)?request
????????????????.getAttribute(?"javax.servlet.include.servlet_path"?);

????????if?(?servletPath?==?null?)

????????
{
????????????servletPath?=?request.getServletPath();
????????}

????????int?beginIdx?=?servletPath.lastIndexOf(?"/"?);
????????int?endIdx?=?servletPath.lastIndexOf(?"."?);

????????StringBuffer?strbuf?=?new?StringBuffer();

????????strbuf.append(?servletPath.substring(?((beginIdx?==?-1)???0
????????????????:?(beginIdx?+?1)),?(endIdx?==?-1)???servletPath.length()?:?endIdx?)?);

????????int?end?=?servletPath.indexOf(?"?",?endIdx?);
????????if?(?end?==?-1?)
????????????end?=?servletPath.length();
????????strbuf.append(?servletPath.substring(?endIdx,?end?)?);

????????pageJumpParameters?=?getPageJumpParameters(?"pageNo,submit,go"?);
????????for?(?int?i?=?0;?i?<?pageJumpParameters.size();?i++?)

????????
{
????????????Map?map?=?(Map)?pageJumpParameters.get(?i?);
????????????String?paraName?=?(String)?map.get(?"paraName"?);
????????????String?paraVal?=?(String)?map.get(?"paraVal"?);

????????????if?(?i?==?0?)

????????????
{
????????????????strbuf.append(?"?"?);
????????????}
????????????else

????????????
{
????????????????strbuf.append(?"&"?);
????????????}
????????????strbuf.append(?paraName?).append(?"="?).append(?paraVal?);
????????}

????????pageUrl?=?strbuf.toString();

????????log.debug(?"pageUrl?=?"?+?pageUrl?);
????}

????public?String?execute()
????????????throws?Exception

????
{
????????String?result?=?doExecute();
????????getURL();
????????initParam();
????????return?result;
????}


????/**?*//**
?????*?just?like?interface?Action?:?public?String?execute()
?????*?
?????*?@throws?Exception
?????*?@return?String?the?same?as?execute()?interface
?????*/
????protected?abstract?String?doExecute()
????????????throws?Exception;


????/**?*//**
?????*?query?the?item?count
?????*?
?????*?@return?int
?????*/
????protected?abstract?int?queryCount()
????????????throws?Exception;

????protected?void?initParam()
????????????throws?Exception

????
{
????????if?(?pageUrl.indexOf(?"?"?)?>?-1?)
????????????pageUrl?+=?"&";
????????else
????????????pageUrl?+=?"?";

????????itemCount?=?queryCount();
????????pageCount?=?(itemCount?+?pageList?-?1)?/?pageList;
????????if?(?pageNo?>?pageCount?)
????????????pageNo?=?1;

????????if?(?pageNo?>?1?)

????????
{
????????????preUrl?=?"<A?href=\""?+?pageUrl?+?"pageNo="?+?(pageNo?-?1)?+?"\">上一頁</A>";
????????????firstUrl?=?"<A?href=\""?+?pageUrl?+?"pageNo=1\">首頁</A>";
????????}
????????else

????????
{
????????????preUrl?=?"上一頁";
????????????firstUrl?=?"首頁";
????????}

????????if?(?pageNo?<?pageCount?)

????????
{
????????????nextUrl?=?"<A?href=\""?+?pageUrl?+?"pageNo="?+?(pageNo?+?1)
????????????????????+?"\">下一頁</A>";
????????????lastUrl?=?"<A?href=\""?+?pageUrl?+?"pageNo="?+?pageCount?+?"\">尾頁</A>";
????????}
????????else

????????
{
????????????nextUrl?=?"下一頁";
????????????lastUrl?=?"尾頁";
????????}
????}

????private?List<Map>?getPageJumpParameters(?String?para_disuse?)

????
{
????????List<Map>?result?=?new?LinkedList<Map>();
????????if?(?para_disuse?==?null?||?para_disuse.trim().length()?==?0?)

????????
{
????????????para_disuse?=?"pageNo,submit";
????????}
????????HttpServletRequest?request?=?ServletActionContext.getRequest();
????????String?para_name?=?"";?//?參數名稱
????????Enumeration?em?=?request.getParameterNames();
????????para_disuse?=?para_disuse.toLowerCase();
????????while?(?em.hasMoreElements()?)

????????
{
????????????para_name?=?(String)?em.nextElement();
????????????if?(?para_disuse.indexOf(?para_name.toLowerCase()?)?==?-1?)?//?先轉換參數名稱為小寫,再比較

????????????
{
????????????????String?para_val?=?request.getParameter(?para_name?)?==?null???""
????????????????????????:?request.getParameter(?para_name?);
????????????????if?(?para_val.trim().length()?>?0?)

????????????????
{
????????????????????log.debug(?para_name?+?"="?+?para_val?);
????????????????????Map<String,?String>?para_map?=?new?HashMap<String,?String>();
????????????????????para_map.put(?"paraName",?para_name?);
????????????????????para_map.put(?"paraVal",?para_val.toString()?);
????????????????????result.add(?para_map?);
????????????????}
????????????}
????????}

????????return?result;
????}

????public?int?getItemCount()

????
{
????????return?itemCount;
????}

????public?String?getNextUrl()

????
{
????????return?nextUrl;
????}

????public?int?getPageCount()

????
{
????????return?pageCount;
????}

????public?List?getPageJumpParameters()

????
{
????????return?pageJumpParameters;
????}

????public?int?getPageList()

????
{
????????return?pageList;
????}

????public?int?getPageNo()

????
{
????????return?pageNo;
????}

????public?String?getPageUrl()

????
{
????????return?pageUrl;
????}

????public?String?getPreUrl()

????
{
????????return?preUrl;
????}

????public?void?setItemCount(?int?itemCount?)

????
{
????????this.itemCount?=?itemCount;
????}

????public?void?setNextUrl(?String?nextUrl?)

????
{
????????this.nextUrl?=?nextUrl;
????}

????public?void?setPageCount(?int?pageCount?)

????
{
????????this.pageCount?=?pageCount;
????}

????public?void?setPageJumpParameters(?List?pageJumpParameters?)

????
{
????????this.pageJumpParameters?=?pageJumpParameters;
????}

????public?void?setPageList(?int?pageList?)

????
{
????????this.pageList?=?pageList;
????}

????public?void?setPageNo(?int?pageNo?)

????
{
????????this.pageNo?=?pageNo;
????}

????public?void?setPageUrl(?String?pageUrl?)

????
{
????????this.pageUrl?=?pageUrl;
????}

????public?void?setPreUrl(?String?preUrl?)

????
{
????????this.preUrl?=?preUrl;
????}

????public?String?getFirstUrl()

????
{
????????return?firstUrl;
????}

????public?void?setFirstUrl(?String?firstUrl?)

????
{
????????this.firstUrl?=?firstUrl;
????}

????public?String?getLastUrl()

????
{
????????return?lastUrl;
????}

????public?void?setLastUrl(?String?lastUrl?)

????
{
????????this.lastUrl?=?lastUrl;
????}
}pagination.jsp:

<%
@?page?contentType="text/html;?charset=GBK"%>

<%
@?taglib?uri="webwork"?prefix="ww"%>

<TABLE?class="Pager"?align="center">
????<TBODY>
????????<TR>
????????????<TD?class="PagerLeftTd">共<ww:property?value="itemCount"?/>項 <ww:property
????????????????value="pageNo"?/>/<ww:property?value="pageCount"?/>頁</TD>
????????????<TD?nowrap></TD>
????????????<TD?class="SpaceWidth"></TD>
????????????<TD?nowrap></TD>
????????????<TD?class="SpaceWidth"></TD>
????????????<TD?nowrap><ww:property?value="firstUrl"?escape="false"?/></TD>
????????????<TD?class="SpaceWidth"></TD>
????????????<TD?nowrap><ww:property?value="preUrl"?escape="false"?/></TD>
????????????<TD?class="SpaceWidth"></TD>
????????????<TD?nowrap><ww:property?value="nextUrl"?escape="false"?/></TD>
????????????<TD?class="SpaceWidth"></TD>
????????????<TD?nowrap><ww:property?value="lastUrl"?escape="false"?/></TD>

????????</TR>
????</TBODY>
</TABLE>