title: JSF: Adding a String to List
body: <p>I have an  JSF 2.0 application that has a bean that holds a list of Strings.</p>
  
  <p>I want to add the String from an <code>&lt;h:inputText&gt;/&gt;</code> to my List and display my list.</p>
  
  <p>The following code just put references in my List. So every element from my List is set to the last input.</p>
  
  <pre><code>@ManagedBean
  @ApplicationScoped
  public class Bean {
  
  private String name;
  private ArrayList&lt;String&gt; test = new ArrayList&lt;String&gt;();
  
  public Bean() {
  }
  
  public Bean(String name) {
      this.name = name;
  }
  
  
  public String addtoList(String _name){
      test.add(_name);
      return "./index.xhtml";
  } 
  
  
  /***************GETTER/SETTER/HASHCODE/EQUALS**************************/
     ...
  
  }
  </code></pre>
  
  <p>here a part of my index.xhtml:</p>
  
  <pre><code>        &lt;h:inputText id="name"
                           value="#{bean.name}"
                           required="true"&gt;
          &lt;/h:inputText&gt;
          &lt;h:commandButton value="Post"  
                           action="#{bean.addtoList(name)}"/&gt;  
          &lt;br/&gt;
          &lt;h:dataTable var="bean"
                       value="#{bean.test}"&gt;
              &lt;h:column&gt;
                  &lt;h:outputText value="#{bean.name}"/&gt;
              &lt;/h:column&gt;
  
          &lt;/h:dataTable&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/2Hl0H.jpg" alt="apllicationExample"></p>