Java-显示列表< Object []>在与struts2的jsp中 [英] Java - Display list<Object[]> in a jsp with struts2

查看:45
本文介绍了Java-显示列表< Object []>在与struts2的jsp中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Struts2上遇到问题.我创建了List<Object[]> myList并用查询结果填充它.

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是什么类型,或者您为Action Bean使用DAO的情况更糟. 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>

这篇关于Java-显示列表&lt; Object []&gt;在与struts2的jsp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆