紧急求助:那里有struts实现翻页功能的介绍文档?

qjhaaaaa 2003-10-17 11:14:05
兄弟们,那位做过strus翻页功能的,能否详细给兄弟讲解
谢谢
...全文
39 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qjhaaaaa 2003-10-18
  • 打赏
  • 举报
回复
如何处理
alan8888 2003-10-17
  • 打赏
  • 举报
回复

kui 2003-10-17
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2228/2228055.xml?temp=.8094751
这里有有关Struts分页的导论。
hj12 2003-10-17
  • 打赏
  • 举报
回复
gz
squallzeng 2003-10-17
  • 打赏
  • 举报
回复
关于这个问题可能每个人有自己的解决办法。
但如果要按照struts的风格来做,应该是这样的:
1) 自己写个类(假定为DataMap),这个类继承HashMap,并实现DynaBean
2) 将ResultSet中的数据取出填充到这个DataMap中
3)将多条数据(也就是多个DataMap)填到一个ArrayList
4) 将这个ArrayList放到你的ActionForm中
5)在jsp中引用, 仿照 struts-example中的例子引用。
例如:
<logic:iterate id="subscription" name="user" property="subscriptions">
<tr>
<td align="left">
<bean:write name="subscription" property="host" filter="true"/>
</td>
<td align="left">
<bean:write name="subscription" property="username" filter="true"/>
</td>
<td align="center">
<bean:write name="subscription" property="type" filter="true"/>
</td>
<td align="center">
<bean:write name="subscription" property="autoConnect"/>
</td>
</tr>
</logic:iterate>
这里subscriptions就是刚才讲的 那个ArrayList
user是ActionForm的名字
subscription就是你那个DataMap的实例,代表一条记录
那些property实际上就是字段名了。

这是一个最小实现:

后台处理代码:
////////////////////////
ResultSet rs = ...
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
ArrayList rows = new ArrayList();
while(rs.next()) {
HashMap row = new HashMap();
for (int i = 1; i <= columnCount; i++) {
String name = rsmd.getColumnName(i);
row.put(name, rs.getObject(i));
}
rows.add(row);
}
///////////////////////////
request.setAttribute("rows",rows); //将包装好的ArrayList放到Request里以便jsp使用。

注意 ////之间的代码其实可作为公用代码,写到组件里去
(传入ResultSet做参数,返回包装好的ArrayList)

前台处理代码:假设这是一个单选框
<logic:iterate id="row" name="rows">
<html:radio property="roleId" value="id" idName="row"/>
<bean:write name="row" property="roleDesc"/>
</logic:iterate>

前台注释:
第1行 这里name="rows"就是后台setAttribute的名字 id="row"是给出循环里要引用每一行的标识
第2行 这里property="roleId"是html表单的变量名
value="id" 是字段的名字
idName="row" 就是第1行的id的值
第3行 这里name="row" 就是第1行的id的值 property="roleDesc"是字段名

如果是要显示这个resultset的记录,方法就跟 struts例子里的基本一样了:

<logic:iterate id="row" name="rows">
<tr>
<td>
<bean:write name="row" property="host"/>
</td>
<td>
<bean:write name="row" property="username"/>
</td>
<td>
<bean:write name="row" property="type"/>
</td>
<td>
<bean:write name="row" property="autoConnect"/>
</td>
</tr>
</logic:iterate>

这里的各项属性意义与上一样。

需要提醒的是:因为我们是将记录集放到request中的,所以这个<logic:iterate标签中不需要使用property属性,struts的例子是把这个记录集放到ActionForm中所以
在他的例子中<logic:iterate的name是ActionForm的名字,property是在ActionForm中定义的记录集(就是我们上面的rows)的名字了.

用struts这样的表示方式还有一些另外的好处,例如可以用bean:write的 format属性
格式化字段的值,这是我们经常要用到的功能,象格式化日期和数字等。

通过这个例子大家可以做到举一反三的效果,凡是需要用到数据库中记录集的地方都可以依此类推,
想下拉表单,多选框等等。
在网上查到的一个较好的解决方案:

As it happens, though, I just checked in a change in commons-beanutils
that can be used for this purpose, if you're using Struts 1.1.
Basically, there's now a really simple way to copy the contents of a
ResultSet into a list of DynaBeans. This really is a *copy* operation,
so you have to pay some performance and memory cost, but it lets you
pass the data without having to create custom beans, and without leaving
the ResultSet open.

You'll need to grab a very recent nightly build of commons-beanutils
from:


http://jakarta.apache.org/builds/jakarta-commons/nightly/commons-beanuti
ls

and replace your commons-beanutils.jar file if you want to try this.

> Please include the .java and .jsp code.
>

In your Java code, you'd do something like this:

Connection conn = ...; // Get connection from the pool
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select custid, name from
customers");
RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
stmt.close();
... return connection to the pool ...
request.setAttribute("custoemrs", rsdc.getRows());

In your JSP page, treat this List of DynaBeans like you would any other
collection of beans, because all the Struts tags know how to deal with
DynaBeans.

<table>
<tr>
<th>Customer ID</th>
<th>Customer Name</th>
</tr>
<logic:iterate name="customers" id="customer">
<tr>
<td><bean:write name="customer" property="custid"/></td>
<td><bean:write name="customer" property="name"/></td>
</tr>
</logic:iterate>
</table>

See the updated Javadocs for commons-beanutils (included in the binary
distribution) for class org.apache.commons.beanutils.RowSetDataSource
for more information about this class.

Craig
或者进这个地方看一下
http://www.hnitline.com/bbs/dispbbs.asp?boardID=18&ID=807
很好的方法
比特灵 2003-10-17
  • 打赏
  • 举报
回复
顶!!!!!!!!!!我也想知道

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧