views:

910

answers:

3

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?

A: 

You can use JSTL with 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.

duffymo
A: 

Struts 1.x takes care of your inner properties like this.

< logic:iterate id="user" name="userList"> < bean:write property="${userName}"/ > < /logic:iterate>

I guess as per ur example u can have. You may not need "#list." again in the value attribute < s:iterator value="users" id="list"> < s:property value="userName" /> < / s:iterator>

kadalamittai
A: 

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, the syntax is

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

leonbloy
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
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. None of the other String properties work, but the int property does...
Kevin
Does you package.entity.User class has a sensible toString() method?And has it a getUsername() method?
leonbloy
I figured it out... the username method was misspelled... Everywhere. Grr. Thanks for your help leonbloy.
Kevin