Often you can experience some troubles accessing correctly the objects (and their properties) on which you’re iterating through a iterator tag.

Let’s consider this action that simply returns an Item list obtained from our business service.

1
2
3
4
5
6
7
8
9
10
11
12
public class MyAction extends ActionSupport {
  private List<MyItem> myList;
  private MyService myService;
 
  public String myMethod() {
    myList = myService.getItemList();
    return ActionSupport.SUCCESS;
 }
  public List<MyItem> getMyList() {
    return myList;
  }
}

The MyItem bean class:

1
2
3
4
5
6
7
8
9
public class MyItem {
  private String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

In the jsp we iterate on myList (that results in the execution of getMyList() in the action) and we try to display the name property of each item (defined in the id attribute of the iterator tag) that calls the getName() method of the Item bean.

These are the results:

1
2
3
4
5
6
7
8
9
<s:iterator value="myList" status="status" id="item" > 
<s:property value="%{attr.item.name}"/> Doesn't work 
<s:property value="%{#attr.item.name}"/> Works 
<s:property value="attr.item.name"/> Doesn't work 
<s:property value="#attr.item.name"/> Works 
<s:property value="item.name"/> Doesn't work 
<s:property value="name"/> Works 
<s:property value="#attr.item.name"/> Works 
</s:iterator>

I hope this little test can be useful.



3 Comments

  1. #
    Balaji Ranjan
    May 21st, 2009 at 4:06 pm

    This tutorial is cool. Thank you very much

    Reply to this comment
  2. #
    zuzu
    August 23rd, 2009 at 11:31 am

    what the hell is ‘attr’ here ??

    Reply to this comment
  3. #
    lirien
    April 25th, 2010 at 8:34 pm

    Thank you, this was really helpful.

    Reply to this comment

Leave a Comment

blank