struts标签库使用问题:如何将使用hibernate查询结果用iterate标签显示出来?

WorldCreater 2003-08-24 04:41:45
请看源码。

想将 for(;iterator.hasNext();)
后的显示代码使用标签显示出来。

<%
// Session s = null;
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
out.println ("(Hibernate)SessionFactory OK");
Session s = sessionFactory.openSession();
//s = sessionFactory.openSession();
out.println ("(Hibernate)Session OK");
Transaction transaction = s.beginTransaction();
out.println ("(Hibernate)Transaction OK");
Query query = s.createQuery ("from MemberTable");
out.println ("(Hibernate)Query OK");
query.setFirstResult(0);
out.println ("(Hibernate)setFirstResult OK");
query.setMaxResults(10);
out.println ("(Hibernate)setMaxResults OK");

transaction.commit ();
out.println ("(Hibernate)Transaction Commit OK");

out.println ("+++++++++++++++++++++++++");
List list = query.list();
out.println ("(Hibernate)Query To List OK");
// pageContext.setAttribute("memberList", list, PageContext.PAGE_SCOPE);

Iterator iterator = query.iterate();
out.println ("(Hibernate)Query To Iterator OK");
out.println("results begin");
for (;iterator.hasNext();) {
out.print ("result ...");
MemberTable table = (MemberTable) iterator.next();
out.print (table.getId());
out.print (" ");
out.print (table.getName());
out.print (" ");
out.print (table.getPassword());
out.print (" ");
out.print (table.getQuestion());
out.print (" ");
out.print (table.getAnswer());
}
out.println("results end");
s.close();
} catch (Exception e) {
out.println ("<<<(Hibernate)Error>>>");
//e.printStackTrace ();
out.println (e);
} finally {
// try { s.close(); } catch (Exception e) {}
}
%>
...全文
48 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
whyxx 2003-08-25
  • 打赏
  • 举报
回复
这还不简单,这样写就行了
<bean:define id="list" name="FormName" property="detailsList" scope="request" />
<logic:iterate id="table" collection="<%=list%>" name="list">
<TD><bean:write name="table" property="id"/></TD>
<TD><bean:write name="table" property="name"/></TD>
<TD><bean:write name="table" property="password"/></TD>
<logic:iterate/>
WorldCreater 2003-08-25
  • 打赏
  • 举报
回复
问题解决:
<html:html locale="true">
<head>
</head>

<body>
<center>

<%--
<logic:iterate name="the name which set by setAttribute() method"
id="the name that you will use in repetition of <logic:iterate></logic:iterate>"
type="the class name of the objects in collection"
scope="request | page | session | application"/>
--%>

<table border="1" width="100%">
<logic:iterate name="memberList" id="xxx" scope="page" type="creatxr.demos.struts.services.db.MemberTable">
<tr>
<td width="25%"><bean:write name="xxx" property="id"/></td>
<td width="25%"><bean:write name="xxx" property="name"/></td>
<td width="25%"><bean:write name="xxx" property="password"/></td>
<td width="25%"><bean:write name="xxx" property="question"/></td>
<td width="25%"><bean:write name="xxx" property="answer"/></td>
</tr>
</logic:iterate>
</table>
<center>
<body>
</html:html>
WorldCreater 2003-08-25
  • 打赏
  • 举报
回复
那么怎处理好?
使用 hibernate 持久化类得到数据集(java.util.List或java.util.Iterator)后,怎么把它用jsp页面显示出来?
smartBlue 2003-08-24
  • 打赏
  • 举报
回复
最好不要把 business logic 的东西放在 jsp 中,这样不符合 struts 的原则
WorldCreater 2003-08-24
  • 打赏
  • 举报
回复
我不会的是:list里是一个个类对象,你看我的源码,想取出对象内容首先要进行强制转换的。

for (;iterator.hasNext();) {
out.print ("result ...");
MemberTable table = (MemberTable) iterator.next();
out.print (table.getId());
out.print (" ");
out.print (table.getName());
out.print (" ");
out.print (table.getPassword());
out.print (" ");
out.print (table.getQuestion());
out.print (" ");
out.print (table.getAnswer());
}
whyxx 2003-08-24
  • 打赏
  • 举报
回复
iterate标签通常支持是List,Map,数组等,你可以将for(;iterator.hasNext();)
存到一个List里面,然后再用iterate标签来显示

67,512

社区成员

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

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