title: How to bind List<Integer> values to selectManyListbox in JSF
body: <p><strong>The situation</strong>:
  I have a JavaServer Faces page and a session-scoped managed bean that has two <code>ArrayList&lt;Integer&gt;</code> properties: one for holding a list of possible values and another for holding a list of selected values. On the JSF page there is a <code>&lt;h:selectManyListBox&gt;</code> component with these two properties bound.</p>
  
  <p><strong>The problem</strong>: after submitting the form the selected values will be converted to string (the property of type ArrayList actually holds a couple of strings!); however, when I use a  converter, I get an error message like this:</p>
  
  <blockquote>
    <p>Validation Error: Value is not valid</p>
  </blockquote>
  
  <p><strong>The question</strong>: How can I bind an <code>ArrayList&lt;Integer&gt;</code> property to the <code>&lt;h:selectManyListBox&gt;</code> component properly?</p>
  
  <p>Thank you for your kind helping me.</p>
  
  <p><strong>The concrete codes</strong></p>
  
  <p>The JSF page:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"&gt;
      &lt;h:body&gt;
          &lt;h:form&gt;
              &lt;h:selectManyListbox value="#{testBean.selection}"&gt;
                  &lt;f:selectItems value="#{testBean.list}"&gt;&lt;/f:selectItems&gt;
              &lt;/h:selectManyListbox&gt;
              &lt;h:commandButton action="#{testBean.go}" value="go" /&gt;
              &lt;ui:repeat value="#{testBean.selection}" var="i"&gt;
                  #{i}: #{i.getClass()}
              &lt;/ui:repeat&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And the managed bean:</p>
  
  <pre><code>import java.io.Serializable;
  import java.util.ArrayList;
  
  @javax.faces.bean.ManagedBean
  @javax.enterprise.context.SessionScoped
  public class TestBean implements Serializable
  {
      private ArrayList&lt;Integer&gt; selection;
      private ArrayList&lt;Integer&gt; list;
  
      public ArrayList&lt;Integer&gt; getList()
      {
          if(list == null || list.isEmpty())
          {
              list = new ArrayList&lt;Integer&gt;();
              list.add(1);
              list.add(2);
              list.add(3);
          }
          return list;
      }
  
      public void setList(ArrayList&lt;Integer&gt; list)
      {
          this.list = list;
      }
  
      public ArrayList&lt;Integer&gt; getSelection()
      {
          return selection;
      }
  
      public void setSelection(ArrayList&lt;Integer&gt; selection)
      {
          this.selection = selection;
      }
  
      public String go()
      {
              // This throws an exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
              /*for (Integer i : selection)
              {
                  System.out.println(i);
              }*/
          return null;
      }
  }
  </code></pre>