Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Put the bean in the view scope and get rid of any business logic in getter methods. </p> <p>The bean must be placed in the view scope so that all previous selections and new available items are remembered, otherwise things will fail if e.g. <code>rendered</code> attribute depends on a condition which was only set in a previous request, or if JSF needs to validate the selected item against list of available items.</p> <p>The getter methods should not contain any business logic as they will also be invoked during a.o. validations phase. You should use <code>&lt;f:ajax listener&gt;</code> to perform business logic based on a change. You should in the listener method also explicitly clear out selected values of child dropdowns. You can use <code>&lt;f:ajax render&gt;</code> to update the contents of child dropdowns. </p> <p>Thus, so:</p> <pre class="lang-html prettyprint-override"><code>&lt;h:selectOneMenu id="state" value="#{nodes.selectedState}"&gt; &lt;f:selectItem itemValue="#{null}" itemLabel="-- select --" /&gt; &lt;f:selectItems value="#{nodes.availableStates}" /&gt; &lt;f:ajax listener="#{nodes.changeState}" render="city region station" /&gt; &lt;/h:selectOneMenu&gt; &lt;h:selectOneMenu id="city" value="#{nodes.selectedCity}"&gt; &lt;f:selectItem itemValue="#{null}" itemLabel="-- select --" /&gt; &lt;f:selectItems value="#{nodes.availableCities}" /&gt; &lt;f:ajax listener="#{nodes.changeCity}" render="region station" /&gt; &lt;/h:selectOneMenu&gt; &lt;h:selectOneMenu id="region" value="#{nodes.selectedRegion}"&gt; &lt;f:selectItem itemValue="#{null}" itemLabel="-- select --" /&gt; &lt;f:selectItems value="#{nodes.availableRegions}" /&gt; &lt;f:ajax listener="#{nodes.changeRegion}" render="station" /&gt; &lt;/h:selectOneMenu&gt; &lt;h:selectOneMenu id="station" value="#{nodes.selectedStation}"&gt; &lt;f:selectItem itemValue="#{null}" itemLabel="-- select --" /&gt; &lt;f:selectItems value="#{nodes.availableStations}" /&gt; &lt;/h:selectOneMenu&gt; </code></pre> <p>with</p> <pre><code>@ManagedBean @ViewScoped public class Nodes { private String selectedState; // getter+setter private String selectedCity; // getter+setter private String selectedRegion; // getter+setter private String selectedStation; // getter+setter private List&lt;SelectItem&gt; availableStates; // getter (no setter necessary!) private List&lt;SelectItem&gt; availableCities; // getter (no setter necessary!) private List&lt;SelectItem&gt; availableRegions; // getter (no setter necessary!) private List&lt;SelectItem&gt; availableStations; // getter (no setter necessary!) @EJB private SomeService someService; @PostConstruct public void init() { availableStates = someService.listStates(); } public void changeState(AjaxBehaviorEvent event) { availableCities = someService.listCities(selectedState); selectedCity = selectedRegion = selectedStation = null; availableRegions = availableStations = null; } public void changeCity(AjaxBehaviorEvent event) { availableRegions = someService.listRegions(selectedCity); selectedRegion = selectedStation = null; availableStations = null; } public void changeRegion(AjaxBehaviorEvent event) { availableStations = someService.listStations(selectedRegion); selectedStation = null; } // Generate necessary getters+setters here. You should not change them. } </code></pre> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope">How to choose the right bean scope?</a></li> <li><a href="https://stackoverflow.com/questions/11360030/best-way-to-add-a-nothing-selected-option-to-a-selectonemenu-in-jsf/">Best way to add a &quot;nothing selected&quot; option to a selectOneMenu in JSF</a></li> <li><a href="https://stackoverflow.com/questions/6848970/how-to-populate-options-of-hselectonemenu-from-database">How to populate options of h:selectOneMenu from database?</a></li> <li><a href="https://stackoverflow.com/questions/11879138/when-to-use-valuechangelistener-or-fajax-listener">When to use valueChangeListener or f:ajax listener?</a></li> <li><a href="https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/">Why JSF calls getters multiple times</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload