<s:iterator ... >使用的例子,STRUTS2.新手,求指教!
ACTION:........publicStringexecute()throwsException{Connectionconn=null;PreparedState...
ACTION:
........
public String execute() throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
@SuppressWarnings({ "unchecked", "rawtypes" })
Map<String, String> request = (Map) ActionContext.getContext().get(
"request");
String rets = "";
int i = 0;
conn = (Connection) ConnDemo.getConnections();
String sqls = "select * from domaininfo where state='0'";// 0,有效,1,无效
pstmt = (PreparedStatement) conn.prepareStatement(sqls);
rst = pstmt.executeQuery();
while (rst.next()) {
i++;
setDomain(rst.getString("domain"));
setBuyby(rst.getString("buyby"));
setMagurl(rst.getString("magurl"));
setPrice(rst.getString("price"));
setBelongs(rst.getString("belong"));
setContest(rst.getString("contest"));
setRegdate(rst.getString("regdate"));
setEnddate(rst.getString("enddate"));
resultList.add(domain);
resultList.add(buyby);
resultList.add(magurl);
resultList.add(price);
resultList.add(belongs);
resultList.add(contest);
resultList.add(regdate);
resultList.add(enddate);
//System.out.print(i);
}
if (i == 0) {
request.put("errorstr", "nodates");
rets = ERROR;
} else {
rets = SUCCESS;
}
//System.out.print(i);//test
//System.out.print(resultList);//test
return rets;
}
List<String> resultList = new ArrayList<String>();
public List<String> getResultList() {
return resultList;
}
public void setResultList(List<String> resultList) {
this.resultList = resultList;
}
..........
============== jsp ===============
</s:else>
<td width="" height=""><s:property value="#L.index+1"/></td>
<td width="" height=""><s:property value="domain"/></td>
<td width="" height=""><s:property value="buyby"/></td>
<td width="" height=""><s:property value="magurl"/></td>
<td width="" height=""><s:property value="price"/></td>
<td width="" height=""><s:property value="belongs"/></td>
<td width="" height=""><s:property value="contest"/></td>
<td width="" height=""><s:property value="regdate"/></td>
<td width="" height="">"<s:property value="enddatec"/></td>
<td width="" height="">操作</td>
</tr>
</s:iterator>
=========结果===========
结果就是循环显示了最后一条记录,而且循环了96次(共12条记录)
==========jsp============
<s:iterator value="#request.resultList" id="domain" status="L">
<s:if test="#L.odd == true">
<tr>
</s:if>
<s:else >
<tr bgcolor="#CCCCFF">
</s:else>
<td width="" height=""><s:property value="#L.index+1"/></td>
<td width="" height=""><s:property value="domain"/></td>
<td width="" height=""><s:property value="buyby"/></td>
<td width="" height=""><s:property value="magurl"/></td>
<td width="" height=""><s:property value="price"/></td>
<td width="" height=""><s:property value="belongs"/></td>
<td width="" height=""><s:property value="contest"/></td>
<td width="" height=""><s:property value="regdate"/></td>
<td width="" height="">"<s:property value="enddatec"/></td>
<td width="" height="">操作</td>
</tr>
</s:iterator> 展开
........
public String execute() throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
@SuppressWarnings({ "unchecked", "rawtypes" })
Map<String, String> request = (Map) ActionContext.getContext().get(
"request");
String rets = "";
int i = 0;
conn = (Connection) ConnDemo.getConnections();
String sqls = "select * from domaininfo where state='0'";// 0,有效,1,无效
pstmt = (PreparedStatement) conn.prepareStatement(sqls);
rst = pstmt.executeQuery();
while (rst.next()) {
i++;
setDomain(rst.getString("domain"));
setBuyby(rst.getString("buyby"));
setMagurl(rst.getString("magurl"));
setPrice(rst.getString("price"));
setBelongs(rst.getString("belong"));
setContest(rst.getString("contest"));
setRegdate(rst.getString("regdate"));
setEnddate(rst.getString("enddate"));
resultList.add(domain);
resultList.add(buyby);
resultList.add(magurl);
resultList.add(price);
resultList.add(belongs);
resultList.add(contest);
resultList.add(regdate);
resultList.add(enddate);
//System.out.print(i);
}
if (i == 0) {
request.put("errorstr", "nodates");
rets = ERROR;
} else {
rets = SUCCESS;
}
//System.out.print(i);//test
//System.out.print(resultList);//test
return rets;
}
List<String> resultList = new ArrayList<String>();
public List<String> getResultList() {
return resultList;
}
public void setResultList(List<String> resultList) {
this.resultList = resultList;
}
..........
============== jsp ===============
</s:else>
<td width="" height=""><s:property value="#L.index+1"/></td>
<td width="" height=""><s:property value="domain"/></td>
<td width="" height=""><s:property value="buyby"/></td>
<td width="" height=""><s:property value="magurl"/></td>
<td width="" height=""><s:property value="price"/></td>
<td width="" height=""><s:property value="belongs"/></td>
<td width="" height=""><s:property value="contest"/></td>
<td width="" height=""><s:property value="regdate"/></td>
<td width="" height="">"<s:property value="enddatec"/></td>
<td width="" height="">操作</td>
</tr>
</s:iterator>
=========结果===========
结果就是循环显示了最后一条记录,而且循环了96次(共12条记录)
==========jsp============
<s:iterator value="#request.resultList" id="domain" status="L">
<s:if test="#L.odd == true">
<tr>
</s:if>
<s:else >
<tr bgcolor="#CCCCFF">
</s:else>
<td width="" height=""><s:property value="#L.index+1"/></td>
<td width="" height=""><s:property value="domain"/></td>
<td width="" height=""><s:property value="buyby"/></td>
<td width="" height=""><s:property value="magurl"/></td>
<td width="" height=""><s:property value="price"/></td>
<td width="" height=""><s:property value="belongs"/></td>
<td width="" height=""><s:property value="contest"/></td>
<td width="" height=""><s:property value="regdate"/></td>
<td width="" height="">"<s:property value="enddatec"/></td>
<td width="" height="">操作</td>
</tr>
</s:iterator> 展开
2个回答
展开全部
问题原因是 12 * 8 = 96
你的java 循环里面一次循环添加了8次数据。。最后的结果打印出来应该是阶梯形式,每行显示数据库中每天数据的某一个字段..
解决: 需要新建一个java bean 保存整条数据的所有字段。 提供get set 方法。 然后在每次循环中new出来。 接着添加到list中...
你的java 循环里面一次循环添加了8次数据。。最后的结果打印出来应该是阶梯形式,每行显示数据库中每天数据的某一个字段..
解决: 需要新建一个java bean 保存整条数据的所有字段。 提供get set 方法。 然后在每次循环中new出来。 接着添加到list中...
追问
我是超级新手,你说的我不太懂,但是我前天找到了原因,也就是你说的这个.问题应该是没有了.你看:
while (rst.next()) {
i++;
domain.dmBean dm = new domain.dmBean();
dm.setDomain(rst.getString("domain"));
dm.setBuyby(rst.getString("buyby"));
dm.setMagurl(rst.getString("magurl"));
.....
list.add(dm);
}
ServletActionContext.getRequest().setAttribute("domlist", list);
追答
对 就是这样弄.. 能自己找出来, 说明你会变强的..
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询