显示列表<Object[]>在带有 Struts 2 的 JSP 中 [英] Displaying List<Object[]> in a JSP with Struts 2
问题描述
我在使用 Struts2 时遇到问题.我创建了一个 List
并用查询结果填充它.
I have an issue with Struts2. I created a List<Object[]> myList
and filled it with a query result.
我的查询从两个表中获取字段,因此我无法将结果放在 bean 实例上(我猜).我想通过迭代器在带有 Struts2 的 JSP 上显示 myList
但我无法获取列表的值.
My query get fields from two tables so I can't put the result on a bean instance (I guess).
I would like to display myList
on a JSP with Struts2 trough an iterator but I can't get the values of the list.
在 DAO 上(我使用 Hibernate):
On the DAO (I use Hibernate):
List<Object[]> myList = session.createQuery("select a.name, b.description, c.description from test a, test2 b where a.id = b.id ");
在 JSP 上,如果我使用此代码:
On the JSP, If I use this code:
<s:iterator value="myList">
<tr>
<td><s:property/></td>
<td><s:property/></td>
</tr>
它只显示列表的一列.
我试过了
<s:iterator value="myList" var="unElem">
<td><s:property value="unElem.name"/></td>
<td><s:property value="%{#unElem.description}" /></td>
<td><s:property value="%{unElem.name}" /></td>
<td><s:property value="%{#unElem.description}" /></td>
</s:iterator>
但它不起作用.你有想法吗 ?
But it's not working. Do you have an idea ?
谢谢.
推荐答案
您列出与迭代器标签一起使用的元素可能包含 Object[]
.它不清楚 myList
是什么类型,或者您正在将 DAO 用于更糟的 action bean.Struts 可以使用 OGNL 表示法显示这些对象,但是如果您尝试提交值,您将无法重新填充该列表.要显示 Object[]
的值,您只需通过列索引访问它们.
You list elements which you are used with the iterator tag might contain Object[]
. It doesn't clear what type is myList
or you are using DAO for your action bean that is worse. Struts can display those objects using OGNL notation but you will not be able to populate that list back if you will try to submit values. To display values of Object[]
you just need to access them by the column index.
<table>
<thead>
<tr>
<th>Name:</th>
<th>Description:</th>
<th>Description:</th>
</tr>
</thead>
<tbody>
<s:iterator value="myList" var="unElem">
<tr>
<td><s:property value="%{#unElem[0]}"/></td>
<td><s:property value="%{#unElem[1]}"/></td>
<td><s:property value="%{#unElem[2]}"/></td>
</tr>
</s:iterator>
</tbody>
</table>
这篇关于显示列表<Object[]>在带有 Struts 2 的 JSP 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!