title: How to save a list/map/set in JSF with h:inputText + managed bean
body: <p>What I'm trying to achieve is very similar to the one posted in the following link. </p>
  
  <p><a href="https://stackoverflow.com/questions/5252708/how-to-save-an-array-in-jsf-with-uirepeat-hinputtext-managed-bean">How to save an array in JSF with ui:repeat + h:inputText + managed bean?</a></p>
  
  <p>I'm particularly fascinated with the answer provided by Arjan Tijms in the link above however what I want to achieve is slightly different. Consider the following code snippets.</p>
  
  <p>The bean</p>
  
  <pre><code>import javax.annotation.PostConstruct;
  import javax.inject.Named;
  import javax.enterprise.context.RequestScoped;
  
  @RequestScoped
  @Named
  public class MyBean {
  
      List&lt;String&gt; choices;
  
      public List&lt;String&gt; getChoices() {
          return choices;
      }
  
      @PostConstruct
      public void initChoices() {
          choices= new ArrayList&lt;String&gt;();
      }
  
      public String save() {
          // should save all the choices into some repository
          return "";
      }
  }
  </code></pre>
  
  <p>and the facelet page</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"        
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
  
      &lt;h:body&gt;
  
          &lt;h:form&gt;
              &lt;ui:repeat value="#{myBean.choices}" varStatus="status"&gt;            
                  &lt;h:inputText value="#{myBean.choices[status.index]}" /&gt;
              &lt;/ui:repeat&gt;
              &lt;h:commandButton value="Save" action="#{myBean.save}" /&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The thing is, this will work if we have some initial data in the list at the beginning. What about situations where the initial list will be empty? </p>
  
  <p>The ideal solution which I'm looking for is to have 1 h:inputText for each choice and when save button is clicked, all choices in each h:inputText is then added to the choices list. I've searched high and low but can't seem to find any hints on how this can be done.</p>
  
  <p>If JSF 2 really doesn't support this, I guess I'll have to use the ugly way with just one h:inputText and use a converter to convert to and from a list but I'm still hoping that an ideal solution can be found.</p>
  
  <p>Hopefully someone from stackoverflow can shed a light in the right direction for me.</p>