struts里的javabean取值问题

mimi5211 2008-11-18 04:47:01
我写了一个从数据库中检索数据的程序,为什么在JSP页面里只能显示最后一个值啊。但是在用System.out.println(qxzmc)却能打印出所有的啊,
下面是程序,大家帮看看啊
qxzActipn:
try{
String sql = "select * from td_qxzdm order by qxzdm";
ConnectDB db=new ConnectDB();
ResultSet rs = db.executeQuery(sql);

//while(rs != null && rs.next())
while(rs.next())
{
//int qxzdm=rs.getInt(qxzdm);

String qxzmc=rs.getString("qxzmc");
String pyip=rs.getString("pyip");
String glip=rs.getString("glip");

request.setAttribute("qxzmc",qxzmc);
request.setAttribute("pyip",pyip);
request.setAttribute("glip",glip);
System.out.println(qxzmc);
QxzBean pb=new QxzBean();
//pb.setQxzdm(qxzdm);
pb.setQxzmc(qxzmc);
pb.setPyip(pyip);
pb.setGlip(glip);
request.setAttribute("qxzbean",pb);
//String ip = request.getRemoteAddr();
//System.out.println(ip);
//response.sendRedirect("sucess.jsp");
}
return mapping.findForward("success");
}
catch(Exception e)
{
request.setAttribute("error","3");
response.sendRedirect("error.jsp");
e.printStackTrace();
}
JSP:
<tr>
<th>权限组名称</th>
<th>评阅IP</th>
<th>管理IP</th><p>
</tr>
<tr>
<td><bean:write name="qxzbean" property="qxzmc"></bean:write></td>
<td><bean:write name="qxzbean" property="pyip"></bean:write></td>
<td><bean:write name="qxzbean" property="glip"></bean:write></td>
</tr>
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
加油馒头 2008-11-23
  • 打赏
  • 举报
回复
while(rs.next())
{
//int qxzdm=rs.getInt(qxzdm);

String qxzmc=rs.getString("qxzmc");
String pyip=rs.getString("pyip");
String glip=rs.getString("glip");

request.setAttribute("qxzmc",qxzmc);
request.setAttribute("pyip",pyip);
request.setAttribute("glip",glip);
System.out.println(qxzmc);
QxzBean pb=new QxzBean();
//pb.setQxzdm(qxzdm);
pb.setQxzmc(qxzmc);
pb.setPyip(pyip);
pb.setGlip(glip);
request.setAttribute("qxzbean",pb);
//String ip = request.getRemoteAddr();
//System.out.println(ip);
//response.sendRedirect("sucess.jsp");
}


随便看了下,你这里是循环吧,每次到 request.setAttribut都重新设值了吧!
所以只保留了最后了一个
加油馒头 2008-11-23
  • 打赏
  • 举报
回复
既然确定值是取出来了

那就是保存显示的问题了,看下怎么保存,怎么显示的
gxiaoqiang1987 2008-11-23
  • 打赏
  • 举报
回复
你在action中应该先声明一个List,然后在while循环里每次创建好一个QxzBean对象并且设置好它的属性后,把它加到List对象中,在循环结束后把List对象放进request中,jsp页面中用logic:iterate 标签或者像这样


<%
List userList = (List)request.getAttribute("userlist");
if (userList == null || userList.size() == 0) {
%>
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
<%
}else {
for (Iterator iter=userList.iterator(); iter.hasNext(); ) {
User user = (User)iter.next();
%>
<tr>
<td><bean:write name="u" property="username"/></td>
<td><bean:write name="u" property="age"/></td>
<td><bean:write name="u" property="group.name"/></td>
</tr>
<%
}
}
%>

fosjos 2008-11-18
  • 打赏
  • 举报
回复
没有循环,所以只有一个
request.setAttribute("qxzbean",pb);每次覆盖前者
要用数组组合一下
jsp用logic:iterate
去找些例子看看吧

67,515

社区成员

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

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