抱歉这篇才写。上班没有多少时间啊!
TableModelBean.java 这是核心业务类,既要被Action使用又要被dwr使用。
由于我注释写了一些,所以就不详细介绍了

public class TableModelBean
{
//表格的第一列
public static final int COLUMN_1 = 0;
//表格的第二列
public static final int COLUMN_2 = 1;
//表格的第三列
public static final int COLUMN_3 = 2;

//每一列的排序升序降序标记 true升序,false降序

private boolean[] columnFlags =
{ false, false, false };
//表格分页总页面数
private int totalPage = 0;
//表格当前页
private int currentPage = 0;
//表格总行数
private int rowsCount = 0;

//没用

private String[] pagers =
{ "" };

//存放全体记录的容器
private List rows = new ArrayList();

//存放当前记录的容器
private List currentPageRows = new ArrayList();

//数据库操作类
private static ModelOneDAO dao;

//每页记录数设为20
private static final int PAGE_SIZE = 20;

//初始排序行为第一行
private int sortedColumn = 1;


/**//**
* 构造函数
*/

public TableModelBean()
{
dao = new ModelOneDAO();
init();
}


/**//**
* 初始化
*/

private void init()
{

try
{
rows = dao.getSortedRows(sortedColumn, columnFlags[sortedColumn]);
setRowsCount(rows.size());
setTotalPage(getTotalPageByRow(rows.size(), PAGE_SIZE));
setCurrentPage(1);

} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**//**
* 返回当前页的内容
* @return Returns the currentPage.
*/

public int getCurrentPage()
{
return currentPage;
}


/**//**
* 设置当前页
* @param currentPage
* The currentPage to set.
*/

public void setCurrentPage(int currentPage)
{
this.currentPage = currentPage;

currentPageRows.clear();
int firstIndex = PAGE_SIZE * (currentPage - 1);
int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
+ PAGE_SIZE : rowsCount;

for (int i = firstIndex; i < lastIndex; i++)
{
currentPageRows.add(rows.get(i));
}
}


/**//**
* 取得所有行
* @return Returns the rows.
*/

public List getRows()
{
return rows;
}



/**//**
* 取的分页数
* @return Returns the totalPage.
*/

public int getTotalPage()
{
init();
return totalPage;
}


/**//**
* 设置分页数
* @param totalPage
* The totalPage to set.
*/

public void setTotalPage(int totalPage)
{
this.totalPage = totalPage;
}


/**//**
* 取得纪录数
* @return Returns the totalRows.
*/

public int getRowsCount()
{
return rowsCount;
}


/**//**
* 设置记录数
* @param totalRows
* The totalRows to set.
*/

public void setRowsCount(int rowsCount)
{
this.rowsCount = rowsCount;
}


/**//**
* 取得当前页中的记录数
* @return Returns the currentPageRows.
*/

public List getCurrentPageRows()
{
return currentPageRows;
}


/**//**
* 取得page页中的记录,当page大于totalPage时返回最后页
* 因为是上面的getCurrentPageRows函数的重载,所以在dwr中不能正常使用。
* 于是出现了getRowsByPageNo方法。
* @param page
* @return the currentPageRows.
*/

public List getCurrentPageRows(int page)
{
currentPageRows.clear();
int firstIndex = PAGE_SIZE * (page - 1);
int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
+ PAGE_SIZE : rowsCount;

for (int i = firstIndex; i < lastIndex; i++)
{
currentPageRows.add(rows.get(i));
}
return currentPageRows;
}


/**//**
* 取得page页中的记录,当page大于totalPage时返回最后页
* @param page
* @return 包含当前页记录的List
*/

public List getRowsByPageNo(int page)
{
init();
page = page > totalPage ? totalPage : page;
List result = new ArrayList();
int firstIndex = PAGE_SIZE * (page - 1);
int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
+ PAGE_SIZE : rowsCount;

for (int i = firstIndex; i < lastIndex; i++)
{
result.add(rows.get(i));
}
return result;
}


/**//**
* 按照某一列进行排序,再返回当前页中的数据
* @param currentPage
* @param columnNo
* @return the Rows of current Page that sorted by columnNo
*/

public List getCurrentPageSortedByColumnRows(int currentPage, int columnNo)
{
init();
sortBy(columnNo);
currentPageRows.clear();
int firstIndex = 20 * (currentPage - 1);
int lastIndex = (firstIndex + 20) < rowsCount ? firstIndex + 20
: rowsCount;

for (int i = firstIndex; i < lastIndex; i++)
{
currentPageRows.add(rows.get(i));
}
return currentPageRows;
}


/**//**
* 返回一个分页数组。用处不太大,客户端用Javascript也可以计算。
* @return Returns the pages.
*/

public String[] getPagers()
{
pagers = new String[totalPage];

for (int i = 1; i <= totalPage; i++)
{
pagers[i - 1] = i + "";
}
return pagers;
}


/**//**
* 按照某一列进行排序
* @param columnNo
*/

public void sortBy(int columnNo)
{
this.sortedColumn = columnNo;
columnFlags[columnNo] = (!columnFlags[columnNo]);

try
{
rows = dao.getSortedRows(columnNo, columnFlags[columnNo]);

} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**//**
* 删除某一列,按照主键(第一列)
* @param key
* @return
*/

public boolean deleteRow(int key)
{

try
{
dao.deleteRow(key);

} catch (SQLException e)
{
e.printStackTrace();
return false;
}
return true;
}


/**//**
* 要新增加一个数据前先计算出Id.
* 这个例子只是用来演示用的,如果多人访问会出现并发问题
* @return
*/

public int getNextId()
{

try
{
return dao.getNextId();

} catch (SQLException e)
{
e.printStackTrace();
return -1;
}
}


/**//**
* 增加一行
* @param trb
* @return
*/

public boolean addRow(TableRowBean trb)
{

try
{
dao.addRow(trb);
return true;

} catch (SQLException e)
{
e.printStackTrace();
return false;
}
}


/**//**
* 更改一行
* @param trb
* @return
*/

public boolean updateRow(TableRowBean trb)
{

try
{
dao.updateRow(trb);
return true;

} catch (SQLException e)
{
e.printStackTrace();
return false;
}
}


/**//**
* 按照key取回单行信息
* @param key
* @return
*/

public TableRowBean getSingleRow(int key)
{
TableRowBean row;

try
{
row = dao.getSingleRow(key);

} catch (SQLException e)
{
row = new TableRowBean();
e.printStackTrace();
}
return row;
}


/**//**
* 辅助方法计算分页数
* @param rowSize
* @param pageSize
* @return
*/

private static int getTotalPageByRow(int rowSize, int pageSize)
{
int result = 0;
result = rowSize % pageSize == 0 ? rowSize / pageSize : rowSize
/ pageSize + 1;
return result;
}
}
接下来就是写配置文件了。主要的配置文件有三个web.xml struts-config.xml dwr.xml
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.mstar.strutsajax.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/dwr.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</web-app>

其中要注意这段Servlet的声明
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/dwr.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
debug打开的话,你就可以看到每一个远程调用。对开发很有用。
struts-config我就不写了,大家对这个比较了解,要看的话,下载我的源码,在最后面我会写上。
重点是dwr.xml.这里是你要提供远程接口信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<init>
<converter id="tablerowbean" class="org.mstar.strutsajax.converter.TableRowConverter"/>
</init>
<allow>
<create creator="new" javascript="UserLogic">
<param name="class" value="org.mstar.strutsajax.ajax.UserLogic"/>
<include method="validate"/>
</create>
<create creator="new" javascript="TableModel" scope="session">
<param name="class" value="org.mstar.strutsajax.form.TableModelBean"/>
<include method="sortBy"/>
<include method="getRowsCount"/>
<include method="getTotalPage"/>
<include method="setCurrentPage"/>
<include method="getCurrentPageRows"/>
<include method="getCurrentPageSortedByColumnRows"/>
<include method="getRowsByPageNo"/>
<include method="deleteRow"/>
<include method="getNextId"/>
<include method="addRow"/>
<include method="updateRow"/>
<include method="getSingleRow"/>
</create>
<convert converter="tablerowbean" match="org.mstar.strutsajax.form.TableRowBean"/>
</allow>
</dwr>
详细配置你可以看dwr的文档。
这里要说的就是,convert,对于你自己的类型如
TableRowBean必须写一个Converter来转化它,我的TableRowConverter其实就是继承与BeanConverter然后什么事情也没做。但是我如果自己用BeanConverter就不行,不知道为什么。其他的都比较好理解。
当然我还有一个weblogic.xml,因为我是发布在weblogic上的。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.//DTD Web Application 8.1//EN" "http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd">
<weblogic-web-app>
<jsp-descriptor>
<jsp-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</jsp-param>
</jsp-descriptor>
<context-root>struts-ajax</context-root>
</weblogic-web-app>
剩下了就是JSP页面了。我再写一篇吧。