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

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

问题描述

我有一个用户类,其中有一个字符串用户名.我有一个用户列表,我试图使用

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>

这些行的显示次数正确,因此它可以正确地遍历我的列表.但是,我不知道如何访问用户名属性来显示它.显然我上面的内容是不正确的......有什么想法吗?

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 中,不推荐使用 id 属性,应该使用 var 来代替(参考)

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

我认为 # 在这里被误用了. 此外,列表"似乎对于每次迭代中要分配的内容来说都是一个坏名字……我认为用户"更合适.

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天全站免登陆