楼主看一下这个行不行?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page import="com.uniland.dao.*" %>
<%@ page import="com.uniland.common.*" %>
<%@ page import="javax.servlet.jsp.PageContext" %>
<html:html>
<HEAD>
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<META http-equiv="Content-Type" content="text/html; charset=GB2312">
<META name="GENERATOR" content="IBM WebSphere Studio">
<LINK href="<%=request.getContextPath()%>/theme/Master.css" type=text/css rel=stylesheet>
<script language="JavaScript">
function popupWindow(id){
var newWin;
newWin = window.open("","","menubar=0,scrollbar=no");
newWin.resizeTo(800,600);
newWin.location = "<%=request.getContextPath()%>/webapps/policy/showpopup.jsp?id="+id;
}
</script>
</HEAD><BODY leftMargin=0 topMargin=0 marginwidth="0">
<table width="586" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colSpan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right" valign="middle" width="81"> </td>
<td class="unnamed2" width="228" align="center" valign="bottom"> </td>
<td width="276" align="right"><img src="<%=request.getContextPath()%>/webapps/images/0809_r5_c29.gif" width="208" height="28"></td>
</tr>
</table>
</td></tr><tr>
<td colSpan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="unnamed2"> <%=(String)request.getAttribute("name")%></td>
</tr>
</table>
</td></tr><tr>
<td width="15"> </td>
<td width="560">
<table width="100%" border="0" bgcolor="#3C81A6" cellspacing="1" cellpadding="0">
<tr>
<td bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="2" class="unnamed2" bgcolor="#E8F5FE">
<tr><td colSpan="2" height="10"> </td></tr>
<% int intRowCount = 0; //记录总数
int intPageCount = 0; //总页数
int intPageSize = Integer.parseInt(Tokens.WEBAPPS_SHOW_LINES); //每页显示的记录数
int intPage; //待显示页码
int i;
RowSet rs = null;
DaoParam myparam;
boolean bNewOpen;// Is 新打开 or 查询 or 提交过来的页面? String strPage = request.getParameter("page");
String strId = request.getParameter("id");
String sFlag = request.getParameter("flag");
System.out.println("page:"+strPage);
System.out.println("id:"+strId);
System.out.println("flag:"+sFlag);
if(strPage == null){
bNewOpen = true;
intPage = 1;
} else {
bNewOpen = false;
intPage = java.lang.Integer.parseInt(strPage);
if(intPage < 1) intPage = 1;
}
if (bNewOpen) { // 第一次打开
System.out.println("page is first opened!");
rs = new RowSet();
if ((strId==null || strId.trim().length()<1) && sFlag!=null && sFlag.trim().length()>0) {
//先得到根节点的id
myparam = new DaoParam();
myparam.set("SERVICE", "WEBAPPBYNAME");
myparam.set("flag", sFlag);
myparam.set("parent_type_id", Tokens.WEBAPPS_ROOTSTRUCT);
rs = CallService.runServiceReturnRowSet(myparam, null);
if (rs!=null && rs.rowCount()>0) {
strId = rs.getValue("TYPE_ID");
myparam = new DaoParam();
myparam.set("SERVICE", "WEBAPPS_LIST");
myparam.set("parent_type_id", strId);
rs = CallService.runServiceReturnRowSet(myparam, null);
request.getSession().setAttribute(Tokens.INFO_BROWSE, rs);
} else { //其下面什么也没有
rs = null;
System.out.println("there's no data!");
}
} else {
//取得其下所有的子目录及文章
myparam = new DaoParam();
myparam.set("SERVICE", "WEBAPPS_LIST");
myparam.set("parent_type_id", strId);
rs = CallService.runServiceReturnRowSet(myparam, null);
request.getSession().setAttribute(Tokens.INFO_BROWSE, rs);
}
} else { // 翻页过来的
System.out.println("page previous or next!");
rs = (RowSet)request.getSession().getAttribute(Tokens.INFO_BROWSE);
} if (rs!=null && rs.rowCount()>0) {
//调整待显示的页码
intRowCount = rs.rowCount();
intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
if(intPage > intPageCount)
intPage = intPageCount; if(intPageCount>0)
{ boolean bInterval = true;
i = 0;
rs.moveTo((intPage-1) * intPageSize); //将记录指针定位到待显示页的第一条记录上
while(i<intPageSize && (i+(intPage-1)*intPageSize)<rs.rowCount())
{%>
<tr><td colSpan="2" <%if(bInterval){%> class="body_bgcolor_blue" <%}%> height="20">
<%if(rs.getValue("FLAG")==null || rs.getValue("FLAG").trim().length()==0) {%>
<a href="javascript:popupWindow(<%=rs.getValue("ID")%>);" class="body_dark_blue">
<img src="<%=request.getContextPath()%>/webapps/images/bz.gif" width="8" height="8" border="0"> <%=rs.getValue("NAME")%></a>
<%} else {%>
<a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?id=<%=rs.getValue("ID")%>&name=<%=(String)request.getAttribute("name")%> > <%=rs.getValue("NAME")%>" class="body_dark_blue">
<img src="<%=request.getContextPath()%>/webapps/images/bz.gif" width="8" height="8" border="0"> <%=rs.getValue("NAME")%></a>
<%}%>
</td></tr><% rs.moveNext();
bInterval = !bInterval;
i++;
}
}%>
<tr><td colSpan="2" height="5"> </td></tr>
<tr><td width="50%" noWrap>共<%=intPageCount%>页 第<%=intPage%>页 共<%=rs.rowCount()%>条</td><td width="50%" noWrap align="right">
<%if(intPage>1){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=1&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>首页<%if(intPage>1){%></a><%}%>
<%if(intPage>1){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=<%=intPage-1%>&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>上一页<%if(intPage>1){%></a><%}%>
<%if(intPage<intPageCount){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=<%=intPage+1%>&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>下一页<%if(intPage<intPageCount){%></a><%}%>
<%if(intPage<intPageCount){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=<%=intPageCount%>&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>尾页<%if(intPage<intPageCount){%></a><%}%>
<a href="javascript:window.history.back();" class="body_dark_blue">返回</a>
</td></tr>
<% } else {%>
<tr><td colSpan="2">没有数据!</td></tr>
<% }
%> </table>
</td>
</tr>
</table>
</td>
<td width="5"> </td>
</tr>
</table>
</BODY>
</html:html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page import="com.uniland.dao.*" %>
<%@ page import="com.uniland.common.*" %>
<%@ page import="javax.servlet.jsp.PageContext" %>
<html:html>
<HEAD>
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<META http-equiv="Content-Type" content="text/html; charset=GB2312">
<META name="GENERATOR" content="IBM WebSphere Studio">
<LINK href="<%=request.getContextPath()%>/theme/Master.css" type=text/css rel=stylesheet>
<script language="JavaScript">
function popupWindow(id){
var newWin;
newWin = window.open("","","menubar=0,scrollbar=no");
newWin.resizeTo(800,600);
newWin.location = "<%=request.getContextPath()%>/webapps/policy/showpopup.jsp?id="+id;
}
</script>
</HEAD><BODY leftMargin=0 topMargin=0 marginwidth="0">
<table width="586" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colSpan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right" valign="middle" width="81"> </td>
<td class="unnamed2" width="228" align="center" valign="bottom"> </td>
<td width="276" align="right"><img src="<%=request.getContextPath()%>/webapps/images/0809_r5_c29.gif" width="208" height="28"></td>
</tr>
</table>
</td></tr><tr>
<td colSpan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="unnamed2"> <%=(String)request.getAttribute("name")%></td>
</tr>
</table>
</td></tr><tr>
<td width="15"> </td>
<td width="560">
<table width="100%" border="0" bgcolor="#3C81A6" cellspacing="1" cellpadding="0">
<tr>
<td bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="2" class="unnamed2" bgcolor="#E8F5FE">
<tr><td colSpan="2" height="10"> </td></tr>
<% int intRowCount = 0; //记录总数
int intPageCount = 0; //总页数
int intPageSize = Integer.parseInt(Tokens.WEBAPPS_SHOW_LINES); //每页显示的记录数
int intPage; //待显示页码
int i;
RowSet rs = null;
DaoParam myparam;
boolean bNewOpen;// Is 新打开 or 查询 or 提交过来的页面? String strPage = request.getParameter("page");
String strId = request.getParameter("id");
String sFlag = request.getParameter("flag");
System.out.println("page:"+strPage);
System.out.println("id:"+strId);
System.out.println("flag:"+sFlag);
if(strPage == null){
bNewOpen = true;
intPage = 1;
} else {
bNewOpen = false;
intPage = java.lang.Integer.parseInt(strPage);
if(intPage < 1) intPage = 1;
}
if (bNewOpen) { // 第一次打开
System.out.println("page is first opened!");
rs = new RowSet();
if ((strId==null || strId.trim().length()<1) && sFlag!=null && sFlag.trim().length()>0) {
//先得到根节点的id
myparam = new DaoParam();
myparam.set("SERVICE", "WEBAPPBYNAME");
myparam.set("flag", sFlag);
myparam.set("parent_type_id", Tokens.WEBAPPS_ROOTSTRUCT);
rs = CallService.runServiceReturnRowSet(myparam, null);
if (rs!=null && rs.rowCount()>0) {
strId = rs.getValue("TYPE_ID");
myparam = new DaoParam();
myparam.set("SERVICE", "WEBAPPS_LIST");
myparam.set("parent_type_id", strId);
rs = CallService.runServiceReturnRowSet(myparam, null);
request.getSession().setAttribute(Tokens.INFO_BROWSE, rs);
} else { //其下面什么也没有
rs = null;
System.out.println("there's no data!");
}
} else {
//取得其下所有的子目录及文章
myparam = new DaoParam();
myparam.set("SERVICE", "WEBAPPS_LIST");
myparam.set("parent_type_id", strId);
rs = CallService.runServiceReturnRowSet(myparam, null);
request.getSession().setAttribute(Tokens.INFO_BROWSE, rs);
}
} else { // 翻页过来的
System.out.println("page previous or next!");
rs = (RowSet)request.getSession().getAttribute(Tokens.INFO_BROWSE);
} if (rs!=null && rs.rowCount()>0) {
//调整待显示的页码
intRowCount = rs.rowCount();
intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
if(intPage > intPageCount)
intPage = intPageCount; if(intPageCount>0)
{ boolean bInterval = true;
i = 0;
rs.moveTo((intPage-1) * intPageSize); //将记录指针定位到待显示页的第一条记录上
while(i<intPageSize && (i+(intPage-1)*intPageSize)<rs.rowCount())
{%>
<tr><td colSpan="2" <%if(bInterval){%> class="body_bgcolor_blue" <%}%> height="20">
<%if(rs.getValue("FLAG")==null || rs.getValue("FLAG").trim().length()==0) {%>
<a href="javascript:popupWindow(<%=rs.getValue("ID")%>);" class="body_dark_blue">
<img src="<%=request.getContextPath()%>/webapps/images/bz.gif" width="8" height="8" border="0"> <%=rs.getValue("NAME")%></a>
<%} else {%>
<a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?id=<%=rs.getValue("ID")%>&name=<%=(String)request.getAttribute("name")%> > <%=rs.getValue("NAME")%>" class="body_dark_blue">
<img src="<%=request.getContextPath()%>/webapps/images/bz.gif" width="8" height="8" border="0"> <%=rs.getValue("NAME")%></a>
<%}%>
</td></tr><% rs.moveNext();
bInterval = !bInterval;
i++;
}
}%>
<tr><td colSpan="2" height="5"> </td></tr>
<tr><td width="50%" noWrap>共<%=intPageCount%>页 第<%=intPage%>页 共<%=rs.rowCount()%>条</td><td width="50%" noWrap align="right">
<%if(intPage>1){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=1&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>首页<%if(intPage>1){%></a><%}%>
<%if(intPage>1){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=<%=intPage-1%>&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>上一页<%if(intPage>1){%></a><%}%>
<%if(intPage<intPageCount){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=<%=intPage+1%>&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>下一页<%if(intPage<intPageCount){%></a><%}%>
<%if(intPage<intPageCount){%><a href="<%=request.getContextPath()%>/webapps/policy/showlist.jsp?page=<%=intPageCount%>&name=<%=(String)request.getAttribute("name")%>" class="body_dark_blue"><%}%>尾页<%if(intPage<intPageCount){%></a><%}%>
<a href="javascript:window.history.back();" class="body_dark_blue">返回</a>
</td></tr>
<% } else {%>
<tr><td colSpan="2">没有数据!</td></tr>
<% }
%> </table>
</td>
</tr>
</table>
</td>
<td width="5"> </td>
</tr>
</table>
</BODY>
</html:html>
那你给出一个不是垃圾的东东来啊?
不过..小呆呆...人家要的是struts的分页.不要搞错了.不过很好...
<logic:iterate id="house" name="house" scope="request">
<tr>
<td><bean:write name="house" property="name" /></td>
<td><bean:write name="house" property="address" /></td>
</tr>
</logic:iterate>你只要创建一个数据bean的数组,用来存放你是结果级,
在你的action子类中把它setRequest就可以了,如:
FormHouse[] outForm = new FormHouse[rs.getRowCount()];
while(rs.moveNext()){
FormHouse fd = new FormHouse();
fd.setName(rs.getString("buld_name"));
fd.setAddress(rs.getString("buld_address"));
outForm[i] = fd;
i++;
}
request.setAttribute("formHouse", outForm);有时间的话,你可以自己给rs写个标签库,直接把rs的结果输出到页面,这样action就很简单了,只要写个sql,得到rs就一切ok了。
很多时候结果集不是取出来就用,可能还要做一些处理再显示吧,这时候你写的rs标签库就不好使了,struts的ActionForm Beans就又发挥作用了。而且,难道你的业务处理只是一句sql那么简单么?难道不需要一些输入判断,合法性校验等等?所以当然还是把这些java语句尽量写在Action里,或者写在你的业务处理bean里由Action调用。写个rs的标签库只是在利用struts的基础上更加简化你的程序,并不与它的基本思想冲突。
我只是顺便提一句,这不是重点。你完全可以不用理会,尽管按照struts的标准流程去做。不过为了解除你的疑惑,我们是如下这般(还是上面那个程序,用自己写的标签库):
action子类中:
String strSql = "SELECT name,address FROM house";
RS rs = db.executeQuery(strSql ); //自己重写了一个处理结果级的类
以下这段将省去:
/*FormHouse[] outForm = new FormHouse[rs.getRowCount()];
while(rs.moveNext()){
FormHouse fd = new FormHouse();
fd.setName(rs.getString("buld_name"));
fd.setAddress(rs.getString("buld_address"));
outForm[i] = fd;
i++;
}
*///输出结果集
request.setAttribute("defaultRS",rs);jsp页面(自己写的标签库):
<rs:list>
<tr>
<td><rs:write name="defaultRS" columName="name" /></td>
<td><rs:write name="defaultRS" columName="address" /></td>
</tr>
</rs:list>因我不是标志库的编写者,所以也不能把他的思路说的很清晰,觉得很可取,所以推荐一下。你的重点问题,你自己不是说了:
在iterate只能用四种集合类型:java.util.Iterator,java.util.Collection,java.util.Map或一个数组。
我的第一个例子中,outForm 对象就是一个数组呀,一个普通的ActionForm类,原码如下:import org.apache.struts.action.ActionForm;public class FormHouse extends ActionForm {
String name = "";
String address = ""; /**
* Returns the address.
* @return String
*/
public String getAddress() {
return address;
} /**
* Returns the name.
* @return String
*/
public String getName() {
return name;
} /**
* Sets the address.
* @param address The address to set
*/
public void setAddress(String address) {
this.address = address;
} /**
* Sets the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}}
照我的第一个例子(Action类和jsp页面的写法都说明了),试试就知道了。没有在struts里调用过ejb,查查ejb的资料吧,应该不是太大问题。
但如果要按照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属性
格式化字段的值,这是我们经常要用到的功能,象格式化日期和数字等。通过这个例子大家可以做到举一反三的效果,凡是需要用到数据库中记录集的地方都可以依此类推,
想下拉表单,多选框等等。
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
lsand 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
在逻辑标记库中定义了<logic:iterate>标记,它能够根据特定集合中元素的数目对标记体的内容进行重复的检查。集合的类型可以是java.util.Iterator,java.util.Collection
,java.util.Map或是一个数组。有三种方法可以定义这个集合:
.使用运行时间表达式来返回一个属性集合的集合
.将集合定义为bean,并且使用name属性指定存储属性的名称。
.使用name属性定义一个bean,并且使用property属性定义一个返回集合的bean属性。
当前元素的集合会被定义为一个页作用域的bean。属性如下,所有这些属性都能使用运行时表达式。
属性 描述
collection 如果没有设置name属性,它就指定了要进行重复的集合
Id 页作用域bean和脚本变量的名称,它保存着集合中当前元素的句柄
indexed 页作用域JSP bean的名称,它包含着每次重复完成后集合的当前索引
Length 重复的最大次数
Name 作为集合的bean的名称,或是一个bean名称,它由property属性定义的属性,是个集合
Offset 重复开始位置的索引
property 作为集合的Bean属性的名称
Scope 如果指定了bean名称,这个属性设置bean的作用域。若没有设置,搜索范围从页到应用程序作用域
Type 为当前定义的页作用域bean的类型例如:
<logic:iterate id="currentInt"
collection="<% =myList %>"
type="java.lang.Integer"
offset="1"
length="2">
<% =currentint %>
</logic:iterate>
代码将从列表中的第一个元素开始重复两个元素并且能够让当前元素作为页作用域和java.lang.Integer类型的脚本变量来使用。也就是说,如果myList包含元素1,2,3,4等,代码将会打印1和2。
<sql:query var="customers"
dataSource="${dataSource}"
maxRows="${maxRows}"
startRow="${startRow}">
SELECT * FROM customers
WHERE country = ’China’
ORDER BY lastname
</sql:query>
<table>
<c:forEach var="row" items="${customers.rows}">
<tr>
<td><c:out value="${row.lastName}"/></td>
<td><c:out value="${row.firstName}"/></td>
<td><c:out value="${row.address}"/></td>
</tr>
</c:forEach>
</table>用两个参数maxRows、startRow就可以控制分页
<logic:iterate offset(好象这么写)控制一下offset就可以达到分页效果。
----------------------------------------
这个offset的参数值怎么修改啊?
我用了offset="<%=off%>",竟然不行
TEST.java
TESTAction.java
TESTDAO.java
TESTForm.java
TEST.jsp
TESTOut.jsp
TEST.java:
package OST;import org.apache.struts.action.ActionForm;public final class TEST extends ActionForm
{
private String address; private String web_site; private String description; private String name; private String password; private String user_id; //myfiledata; public String getUser_id()
{
return (this.user_id);
} public void setUser_id(String user_id)
{
this.user_id=user_id;
}
public String getPassword()
{
return (this.password);
} public void setPassword(String password)
{
this.password=password;
}
public String getName()
{
return (this.name);
} public void setName(String name)
{
this.name=name;
}
public String getDescription()
{
return (this.description);
} public void setDescription(String description)
{
this.description=description;
}
public String getWeb_site()
{
return (this.web_site);
} public void setWeb_site(String web_site)
{
this.web_site=web_site;
}
public String getAddress()
{
return (this.address);
} public void setAddress(String address)
{
this.address=address;
}}TESTAction.java:
package OST;import OST.jdbc.util.ConnectionPool;
import OST.TESTDAO;import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.ModuleException;
import org.apache.struts.util.MessageResources;import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
public final class TESTAction extends Action
{
// 变量定义:
private Log log = LogFactory.getLog("org.apache.struts.webapp.Example"); private ConnectionPool pool; public TESTAction() {
pool = ConnectionPool.getInstance();
}
//函数定义:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Extract attributes and parameters we will need
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
TESTForm m_TESTform = (TESTForm) form;
String variable1= m_TESTform.getMybeanvariable1();
Connection con = null; try
{
con = pool.getConnection();
TESTDAO m_TESTDAO = new TESTDAO(con);
//<sqlstr>
String sql="SELECT * FROM dbo.attraction";
//</sqlstr>
Collection col = m_TESTDAO.findSQL(sql);
request.setAttribute("TEST",col);
//<分页>
m_TESTform.setRowCount(col.size());
m_TESTform.setLength(2);//设置每页显示记录数
String page=request.getParameter("page");
m_TESTform.setMybeanvariable1(page);
int ipage;
try
{
ipage=java.lang.Integer.parseInt(page,10);
}
catch (Exception e)
{
ipage=0;
}
m_TESTform.setPage(ipage);
m_TESTform.setOffset(ipage*m_TESTform.getLength());
m_TESTform.setPageCount(m_TESTform.getRowCount()/m_TESTform.getLength()+1);
String strPage="";
strPage+="共";
strPage+=String.valueOf(m_TESTform.getRowCount());
strPage+="条记录,共";
strPage+=String.valueOf(m_TESTform.getPageCount());
strPage+="页,当前是第";
strPage+=String.valueOf(ipage+1);
strPage+="页, ";
int istart,iend;
istart=ipage-5;
if(istart<0) {istart=0;}
iend=istart+10;
if(iend>m_TESTform.getPageCount()) {iend=m_TESTform.getPageCount();}
istart=iend-10;
if(istart<0) {istart=0;}
for(int i=istart;i<iend;i++)
{
strPage+="<a href='TESTAction.do?page=";
strPage+=String.valueOf(i);
strPage+="'>";
strPage+=String.valueOf(i+1);
strPage+="</a>";
strPage+=" ";
}
m_TESTform.setMybeanvariable1(strPage);
//</分页>
request.setAttribute("TESTForm",m_TESTform);
return mapping.findForward("success");
}
catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException("Unable to get connection.");
}
finally
{
try
{
if (con != null)
con.close();
}
catch (SQLException e)
{
throw new RuntimeException(e.getMessage());
}
} }}
import OST.TEST;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import java.util.Collection;
import java.util.ArrayList;public class TESTDAO { private Connection con; public TESTDAO(Connection con) {
this.con = con;
} public void create(TEST m_TEST) throws SQLException {
PreparedStatement ps = null;
//<createSQL>
String sql = "INSERT INTO dbo.attraction VALUES (?,?,?,?,?,?)";
//</createSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
ps = con.prepareStatement(sql);
//<create>
ps.setString(1,m_TEST.getUser_id());
ps.setString(2,m_TEST.getPassword());
ps.setString(3,m_TEST.getName());
ps.setString(4,m_TEST.getDescription());
ps.setString(5,m_TEST.getWeb_site());
ps.setString(6,m_TEST.getAddress());
//</create> if (ps.executeUpdate() != 1) {
throw new SQLException ("error.create.TEST");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
} public void update(TEST m_TEST) {
PreparedStatement ps = null;
//<updateSQL>
String sql = "UPDATE dbo.attraction SET password = ?,name = ?,description = ?,web_site = ?,address = ? WHERE user_id = ?";
//</updateSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
ps = con.prepareStatement(sql);
//<update>
ps.setString(1,m_TEST.getPassword());
ps.setString(2,m_TEST.getName());
ps.setString(3,m_TEST.getDescription());
ps.setString(4,m_TEST.getWeb_site());
ps.setString(5,m_TEST.getAddress()); ps.setString(6,m_TEST.getUser_id());
//</update> if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.TEST");
} } catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
} public void remove(String sql) {
PreparedStatement ps = null;
try { if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
} ps = con.prepareStatement(sql);
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.TEST");
} } catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
} public TEST findByPrimaryKey(String KeyId)
throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
TEST m_TEST= null; //<findByPrimaryKeySQL>
String sql = "SELECT * from dbo.attraction WHERE user_id = ?";
//</findByPrimaryKeySQL>
try { if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
} ps = con.prepareStatement(sql);
ps.setString(1,KeyId);
rs = ps.executeQuery(); if (rs.next()) {
m_TEST= new TEST();
//<findByPrimaryKey>
m_TEST.setUser_id(rs.getString(1));
m_TEST.setPassword(rs.getString(2));
m_TEST.setName(rs.getString(3));
m_TEST.setDescription(rs.getString(4));
m_TEST.setWeb_site(rs.getString(5));
m_TEST.setAddress(rs.getString(6));
//</findByPrimaryKey> return m_TEST;
} else {
throw new SQLException (
"error.removed.TEST");
} } catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
} public Collection findSQL(String sql) {
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList(); try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
TEST m_TEST= new TEST();
//<find>
m_TEST.setUser_id(rs.getString(1));
m_TEST.setPassword(rs.getString(2));
m_TEST.setName(rs.getString(3));
m_TEST.setDescription(rs.getString(4));
m_TEST.setWeb_site(rs.getString(5));
m_TEST.setAddress(rs.getString(6));
//</find> list.add(m_TEST); } return list; } catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}}----------------------------------
TESTForm.java:package OST;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public final class TESTForm extends ActionForm
{
private String mybeanvariable1; private int offset=0; private int length=2; private int page=0; private int pageCount=0; private int rowCount=0; //myfiledata; public int getRowCount()
{
return (this.rowCount);
} public void setRowCount(int RowCount)
{
this.rowCount=RowCount;
}
public int getPageCount()
{
return (this.pageCount);
} public void setPageCount(int PageCount)
{
this.pageCount=PageCount;
}
public int getPage()
{
return (this.page);
} public void setPage(int Page)
{
this.page=Page;
}
public int getLength()
{
return (this.length);
} public void setLength(int length)
{
this.length=length;
}
public int getOffset()
{
return (this.offset);
} public void setOffset(int offset)
{
this.offset=offset;
}
public String getMybeanvariable1()
{
return (this.mybeanvariable1);
} public void setMybeanvariable1(String mybeanvariable1)
{
this.mybeanvariable1=mybeanvariable1;
}}
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><html:html >
<head>
<title>
</title>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.3790.0" name=GENERATOR>
</head>
<body bgColor=white>
<html:base/> <html:form method="post"
action="/lykAction.do">
<html:text property="mybeanvariable1" /> <html:submit value="提交" /> <html:reset value="重写" />
</html:form >
</body>
</html:html >------------------------------------------
TESTOut.jsp:<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><html:html >
<head>
<title>
</title>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.3790.0" name=GENERATOR>
</head>
<body bgColor=white>
<html:base/>
<table cellPadding=2 width="80%" border=2>
<TBODY>
<tr>
<td>
<span contentEditable=true>user_id</span> </td>
<td>
<span contentEditable=true>password</span> </td>
<td>
<span contentEditable=true>name</span> </td>
<td>
<span contentEditable=true>description</span> </td>
<td>
<span contentEditable=true>web_site</span> </td>
<td>
<span contentEditable=true>address</span> </td>
</tr>
<bean:define
property="offset" name="TESTForm" id="offset" /> <bean:define
property="length" name="TESTForm" id="length" /> <logic:iterate
name="TEST" id="TEST" length="<%= String.valueOf(length) %>"
offset="<%= String.valueOf(offset) %>" scope="request" type="OST.TEST">
<tr>
<td>
<bean:write property="user_id" name="TEST" /> </td>
<td>
<bean:write property="password" name="TEST" /> </td>
<td>
<bean:write property="name" name="TEST" /> </td>
<td>
<bean:write property="description" name="TEST" /> </td>
<td>
<bean:write property="web_site" name="TEST" /> </td>
<td>
<bean:write property="address"
name="TEST" /> </td>
</tr>
</logic:iterate>
<tr>
<TD align=middle colSpan=7>
<bean:write property="mybeanvariable1"
name="TESTForm"
filter="false" /> <br>
</td>
</tr>
</tr>
</TBODY>
</table>
</body>
</html:html >