你如何迭代对象列表? [英] How do you iterate through a list of objects?

查看:120
本文介绍了你如何迭代对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个User类,其中包含一个String用户名。我有一个用户列表,我试图在表中使用

I have a User class that has a String username in it. I have a list of users that I'm trying to display in a table using

                         <s:iterator value="users" id="list">
                                <tr>
                                    <td><s:property value="#list.username" /></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                         </s:iterator>

行显示的次数正确,因此它正在我的列表中正确迭代。但是,我不知道如何访问username属性来显示它。显然我上面的内容是不正确的...任何想法?

The rows are being displayed the right number of times, so it's iterating through my list properly. However, I don't know how to access the username property to display it. Obviously what I have above isn't correct... Any ideas?

推荐答案

首先,在Struts2 2.1.x中<$ p不推荐使用c $ c> id 属性,而应使用 var ref

First, in Struts2 2.1.x the id attribute is deprecated, var should be used instead (ref)

我认为#在那里被误用了。此外,list似乎是每次迭代中分配的错误名称...我认为用户更合适。

I think the # is misused there. Besides, "list" seems a bad name for what is to be assigned in each iteration... I think "user" is more appropiate.

IIRC,语法是

 <s:iterator value="users" var="user">
  ...  <s:property value="#user.username" />
 </s:iterator>

此外,对于这样一个简单的情况,您不需要在迭代器中分配当前项。这也应该有效:

Further, you don't need to assign the current item in the iterator for such a simple case. THis should also work:

 <s:iterator value="users">
  ...  <s:property value="username" />
 </s:iterator>

您也可以尝试这样做:

  <s:iterator value="users">
      ...  <s:property />      <!-- this outputs the full object, may be useful for debugging -->
  </s:iterator>

更新:我纠正了关于#的位,没关系。

UPDATE: I corrected the bit about the #, it was ok.

这篇关于你如何迭代对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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