title: Using depending selection lists in request scope
body: <p>A question we keep getting from users is to have selections (drop-down lists or multi-selects) with dependent values.  For instance, a user would pick a country, then the system populates a city dropdown with the cities in that country.</p>
  
  <p>I've made this work often enough in session (or conversation scope) but now, for a real lightweight scenario, I would like to have it work in request scope.</p>
  
  <p>Here's some dummy code showing the problem (usually we would use A4J to populate the dropdowns without a full refresh, but the problem can be demonstrated with plain jsf as well):</p>
  
  <p><strong>JSF:</strong></p>
  
  <pre><code>&lt;h:form&gt;
      &lt;p&gt;
          &lt;h:selectOneMenu value="#{bean.selectedSourceValue}"&gt;
              &lt;f:selectItems value="#{bean.sourceValues}" /&gt;
          &lt;/h:selectOneMenu&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;h:selectOneMenu value="#{bean.selectedDependentValue}"&gt;
              &lt;f:selectItems value="#{bean.dependentValues}" /&gt;
          &lt;/h:selectOneMenu&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;h:commandButton value="submit" /&gt;
      &lt;/p&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p><strong>Backing bean:</strong></p>
  
  <pre><code>public class Bean {
  
      private Integer selectedSourceValue;
      private Integer selectedDependentValue;
  
      /**
       * @return values for the first selection.  Numbers from 1 to 10.
       */
      public List&lt;SelectItem&gt; getSourceValues(){
          List&lt;SelectItem&gt; r = new ArrayList&lt;SelectItem&gt;();
          for(int i=1; i&lt;=10; i++){
              r.add(new SelectItem(i));
          }
          return r;
      }
  
      /**
       * @return values for the second selection.  First ten powers of the selected first value.
       */
      public List&lt;SelectItem&gt; getDependentValues(){
          if (selectedSourceValue==null) return Collections.emptyList();
          List&lt;SelectItem&gt; r = new ArrayList&lt;SelectItem&gt;();
          for(int i=1; i&lt;=10; i++){
              r.add(new SelectItem((int)Math.pow(selectedSourceValue, i)));
          }
          return r;
      }
  
          // ... snipped some basic getter and setters 
  }
  </code></pre>
  
  <p>Seems simple enough.  The problem is when making the second selection.  When the second dropdown is submitted, the combos are validated.  But in the validation phase, the request-scoped bean is not populated yet, so <code>getDependentValues()</code> returns null.  This causes jsf to throw a <code>NoSuchElementException</code> (using Sun RI).</p>
  
  <p>Any idea on how to solve this, or even whether it's possible?</p>