<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web2.0 Tutorials &#187; iterator</title>
	<atom:link href="http://www.zulutown.com/blog/tag/iterator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zulutown.com/blog</link>
	<description>All the Guides You Need to Become a Web2.0 Expert</description>
	<lastBuildDate>Wed, 24 Aug 2011 10:06:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Accessing current item properties in a Struts2 iterator</title>
		<link>http://www.zulutown.com/blog/2009/01/21/accessing-current-item-properties-in-a-struts2-iterator/</link>
		<comments>http://www.zulutown.com/blog/2009/01/21/accessing-current-item-properties-in-a-struts2-iterator/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 17:21:10 +0000</pubDate>
		<dc:creator>Zulutown Webmaster</dc:creator>
				<category><![CDATA[Struts2]]></category>
		<category><![CDATA[iterator]]></category>

		<guid isPermaLink="false">http://www.zulutown.com/blog/?p=17</guid>
		<description><![CDATA[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. 1 2 3 4 5 6 7 8 9 10 11 12 public class MyAction extends ActionSupport &#123; private [...]]]></description>
			<content:encoded><![CDATA[<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>
<p>Let&#8217;s consider this action that simply returns an <code>Item</code> list obtained from our business service.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAction <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>MyItem<span style="color: #339933;">&gt;</span> myList<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> MyService myService<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    myList <span style="color: #339933;">=</span> myService.<span style="color: #006633;">getItemList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> ActionSupport.<span style="color: #006633;">SUCCESS</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>MyItem<span style="color: #339933;">&gt;</span> getMyList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> myList<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The <code>MyItem</code> bean class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyItem <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<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>
<p>These are the results:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;s:iterator <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myList&quot;</span> status<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;status&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item&quot;</span> &gt;</span> 
<span style="color: #009900;">&lt;s:property <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;%{attr.item.name}&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> Doesn't work 
<span style="color: #009900;">&lt;s:property <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;%{#attr.item.name}&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> Works 
<span style="color: #009900;">&lt;s:property <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;attr.item.name&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> Doesn't work 
<span style="color: #009900;">&lt;s:property <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#attr.item.name&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> Works 
<span style="color: #009900;">&lt;s:property <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item.name&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> Doesn't work 
<span style="color: #009900;">&lt;s:property <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> Works 
<span style="color: #009900;">&lt;s:property <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#attr.item.name&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> Works 
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>s:iterator&gt;</span></pre></td></tr></table></div>

<p>I hope this little test can be useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zulutown.com/blog/2009/01/21/accessing-current-item-properties-in-a-struts2-iterator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

