在某个项目中,经过几个同事的修改, 最终得到这个支持分页的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>