I have an issue with Struts2. I created a List<Object[]> myList and filled it with a query result.

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.

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 "); 

On the JSP, If I use this code:

<s:iterator value="myList">
<tr>    
    <td><s:property/></td>
    <td><s:property/></td>
</tr>

It's display just one column of the list.

I tried this

<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 ?

Thank you.

有帮助吗?

解决方案

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top