{"id":17,"date":"2009-01-21T10:21:10","date_gmt":"2009-01-21T17:21:10","guid":{"rendered":"http:\/\/www.zulutown.com\/blog\/?p=17"},"modified":"2009-01-21T10:22:24","modified_gmt":"2009-01-21T17:22:24","slug":"accessing-current-item-properties-in-a-struts2-iterator","status":"publish","type":"post","link":"http:\/\/www.zulutown.com\/blog\/2009\/01\/21\/accessing-current-item-properties-in-a-struts2-iterator\/","title":{"rendered":"Accessing current item properties in a Struts2 iterator"},"content":{"rendered":"<p>Often you can experience some troubles accessing correctly the objects (and their properties) on which you&#8217;re iterating through a <code>iterator<\/code> tag.<\/p>\n<p>Let&#8217;s consider this action that simply returns an <code>Item<\/code> list obtained from our business service.<\/p>\n<pre lang=\"java\" line=\"1\"> \r\npublic class MyAction extends ActionSupport {\r\n  private List<MyItem> myList;\r\n  private MyService myService;\r\n\r\n  public String myMethod() {\r\n    myList = myService.getItemList();\r\n    return ActionSupport.SUCCESS;\r\n }\r\n  public List<MyItem> getMyList() {\r\n    return myList;\r\n  }\r\n}\r\n<\/pre>\n<p>The <code>MyItem<\/code> bean class:<\/p>\n<pre lang=\"java\" line=\"1\"> \r\npublic class MyItem {\r\n  private String name;\r\n  public String getName() {\r\n    return name;\r\n  }\r\n  public void setName(String name) {\r\n    this.name = name;\r\n  }\r\n}\r\n<\/pre>\n<p>In the jsp we iterate on <code>myList<\/code> (that results in the execution of <code>getMyList()<\/code> in the action) and we try to display the <code>name<\/code> property of each <code>item<\/code> (defined in the <code>id<\/code> attribute of the <code>iterator<\/code> tag) that calls the <code>getName()<\/code> method of the  <code>Item<\/code> bean.<\/p>\n<p>These are the results:<\/p>\n<pre lang=\"html4strict\" line=\"1\"> \r\n<s:iterator value=\"myList\" status=\"status\" id=\"item\" > \r\n<s:property value=\"%{attr.item.name}\"\/> Doesn't work \r\n<s:property value=\"%{#attr.item.name}\"\/> Works \r\n<s:property value=\"attr.item.name\"\/> Doesn't work \r\n<s:property value=\"#attr.item.name\"\/> Works \r\n<s:property value=\"item.name\"\/> Doesn't work \r\n<s:property value=\"name\"\/> Works \r\n<s:property value=\"#attr.item.name\"\/> Works \r\n<\/s:iterator> \r\n<\/pre>\n<p>I hope this little test can be useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often you can experience some troubles accessing correctly the objects (and their properties) on which you&#8217;re iterating through a iterator tag. Let&#8217;s consider this action that simply returns an Item list obtained from our business service. public class MyAction extends &hellip; <a href=\"http:\/\/www.zulutown.com\/blog\/2009\/01\/21\/accessing-current-item-properties-in-a-struts2-iterator\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[10,167],"_links":{"self":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts\/17"}],"collection":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":21,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":38,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/38"}],"wp:attachment":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}