Java - Display list<Object[]> in a jsp with struts2

扶醉桌前 提交于 2019-12-08 12:41:39

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/22431796/java-display-listobject-in-a-jsp-with-struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!