title: Struts2 - how to update a collection in a form
body: <p>I've been researching this for a while. I've read
  <a href="https://stackoverflow.com/questions/5855732/how-to-update-a-the-contents-of-a-list-displayed-on-jsp-using-struts2">How to update a the contents of a list displayed on JSP using Struts2?</a>
  and it is very close to what I need, but I'm still not quite able to get my problem resolved.</p>
  
  <p>I have a java.util.Set of objects. The class is 'Event', and the name of the set is 'events'. I want to present a form in a JSP that allows the user to modify attributes of the Event objects, but I want to update more than one at a time. Here is my JSP snippet :</p>
  
  <pre><code>&lt;s:form theme="simple"&gt;
    &lt;table&gt;
      &lt;s:iterator value="events" var="ev"&gt;
      &lt;tr&gt;
        &lt;td&gt; &lt;s:textfield name="?????" value="%{#ev.price}" &lt;/td&gt;
        &lt;td&gt; &lt;s:textfield name="?????" value="%{#ev.amount}" &lt;/td&gt;
       &lt;/tr&gt;
       &lt;/s:iterator&gt;
       &lt;tr&gt;&lt;td colspan="2"&gt;
         &lt;s:submit value="Apply" action="EditEventsAction_save" &gt;&lt;/s:submit&gt;
       &lt;/td&gt;&lt;/tr&gt;
    &lt;/table&gt;
  &lt;/s:form&gt;
  </code></pre>
  
  <p>My problem is (I think) what needs to go in the name of the texfield in order to update my Set. </p>
  
  <p>How can I specify that the data in the textfield is supposed to update a member of my Set? </p>
  
  <p>Is this possible?</p>
  
  <p><em>UPDATE</em></p>
  
  <p>This is my latest (simplified) attempt, based largely on what I read in the <a href="http://struts.apache.org/2.3.4/docs/type-conversion.html#TypeConversion-Indexingacollectionbyapropertyofthatcollection" rel="nofollow noreferrer">type conversion docs</a>.</p>
  
  <p>The form is populated correctly, but changes entered in the textfield are not captured on submit.</p>
  
  <p>Action class:</p>
  
  <pre><code>public class TestAction extends ActionSupport implements Preparable {
  
      private static HashMap&lt;Integer, AssetId&gt; assetMap = new HashMap&lt;Integer, AssetId&gt;();
  
      public String execute () {
          return SUCCESS;
      }   
  
      public void prepare() throws Exception {
          AssetId a1 = new AssetId(1, "Asset 1");
          AssetId a2 = new AssetId(2, "Asset 2");
          AssetId a3 = new AssetId(3, "Asset 3");
          assetMap.put(1, a1);
          assetMap.put(2, a2);
          assetMap.put(3, a3);
      }
  
      public String save () {
          return SUCCESS;
      }
  
      public static HashMap&lt;Integer, AssetId&gt; getAssetMap() {
          return assetMap;
      }
  
      public static void setAssetMap(HashMap&lt;Integer, AssetId&gt; assetMap) {
          TestAction.assetMap = assetMap;
      }
  }
  </code></pre>
  
  <p>JSP:</p>
  
  <pre><code>&lt;%@ taglib uri="/struts-tags" prefix="s" %&gt;
  
  &lt;html&gt;
    &lt;body&gt;      
      &lt;s:form theme="simple"&gt;
        &lt;table&gt;
          &lt;s:iterator value="assetMap" var="asset"&gt;
            &lt;tr&gt;
              &lt;td&gt;&lt;s:textfield name="assetMap[key].clientId"  /&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/s:iterator&gt;
          &lt;tr&gt;&lt;td colspan="2"&gt;&lt;s:submit value="Apply!" action="TestAction_save" /&gt;&lt;/td&gt;&lt;/tr&gt;
        &lt;/table&gt;
      &lt;/s:form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><code>AssetId</code> class:</p>
  
  <pre><code>public class AssetId implements java.io.Serializable {
      private int assetId;
      private String clientId;
      // And public getters/setters
  }
  </code></pre>
  
  <p><code>TestAction-conversion.properties</code></p>
  
  <pre><code>Key_assetMap=java.lang.Integer
  Element_assetMap=AssetId
  CreateIfNull_assetMap=false
  </code></pre>