[STRUTS 2],你如何遍历一个对象列表 ?

 

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?任何想法?

 
 

First, in Struts2 2.1.x the id attribute is deprecated, var should be used instead ( ref )首先,在Struts2的2.1.x id属性是过时, var应该被用来代替 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, the syntax is 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> 

Also you might want to try this:此外,你可能想尝试:

  <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.更新:我纠正#位,这是确定。

 
Thank you for your reply. 感谢您的答复Using your second suggestion, nothing is showing up for output. 使用你的第二个建议,无非是显示输出。 I'm not sure why... 我不知道为什么... ... When I do your last suggestion, each row in the table looks basically like package.entity.User@d338d3d, but each hex value after the @ is different.Kevin May 2 '10 at 22:46 当我做你的最后建议,表中的每一行基本上看起来像package.entity.User @ d338d3d,但每个@之后的十六进制值是不同的 - 凯文5月2日'10于22时46
 
Also interesting, I have an int property called "id" in my User class that does work if I put "id" where I have "username" above. 同样有趣的是,我有一个int属性,所谓的“身份证”,没有工作,如果我把上面的“用户名”“身份证”,在那里我有我的User类。 None of the other String properties work, but the int property does...Kevin May 2 '10 at 23:08 没有其他的String属性的工作,但int属性 ... -凯文在5月2日 '10 23:08
 
Does you package.entity.User class has a sensible toString() method? package.entity.User类有一个明智的toString()方法,你呢? And has it a getUsername() method?leonbloy May 2 '10 at 23:17 getUsername()方法- leonbloy 5月2日'10于23点17分
 
I figured it out... 我想通了... ... the username method was misspelled... 用户名方法是拼写错误的... ... Everywhere. 无处不在。 Grr. GRR。 Thanks for your help leonbloy.Kevin May 2 '10 at 23:38 感谢您的帮助leonbloy - 凯文5月2 '10于23时38

You can use JSTL with Struts.您可以使用JSTL与Struts。 It has a <c:forEach> tag in its core library that will allow you to iterate through a list or any other collection easily.它有一个<c:forEach>其核心库中的标签,可以让你轻松地迭代通过列表或任何其他集合。

link | improve this answer 链接 | 改善这个答案
 
 

Struts 1.x takes care of your inner properties like this. Struts 1.x的需要这样的照顾你的内在属性。

< logic:iterate id="user" name="userList"> < bean:write property="${userName}"/ > < /logic:iterate> <迭代逻辑:ID =“用户”名称=“用户列表”> <豆:写属性=“$ {U​​SERNAME}”/> </逻辑:迭代>

I guess as per ur example u can have.我想,作为每UR例如可以有。 You may not need "#list."您可能不需要“#列表。” again in the value attribute < s:iterator value="users" id="list"> < s:property value="userName" /> < / s:iterator>再次在value属性<S:迭代器值=“用户”ID =“名单”> <S:属性值=“用户名”/> </ S:迭代>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值