Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>ValueChangeListener</code> will only be called when the form is submitted, not when the value of the input is changed. Thus, if you want to run this listener when the value is modified, you have two solutions:</p> <ol> <li>Submit your form when the <code>onchange</code> event is fired (this is what you did in your code);</li> <li>Use an Ajax call instead, by using some dedicated components (already integrated in JSF2, with <code>&lt;f:ajax&gt;</code>, or third-parties libraries such as Richfaces, Primefaces...).</li> </ol> <p>Here is an example with Richfaces:</p> <pre><code>&lt;h:selectOneMenu id="countries" value="#{countryBean.selectedCountry}" valueChangeListener="#{countryBean.changeCountry}"&gt; &lt;a4j:support event="onchange" .../&gt; &lt;f:selectItems value="#{countryBean.countries }" /&gt; &lt;/h:selectOneMenu&gt; </code></pre> <p>Regarding the code of your listener, it seems correct, but why question is <em>why do you need a ValueChangeListener here</em>? Indeed, this listener is usefull when you want to track a modification of a value. That's why the <code>ValueChangeEvent</code> provides both <code>getOldValue()</code> and <code>getNewValue()</code> methods.</p> <p>In your code, you do not care about the old value, so basically, you could "simply" do an action instead of a <code>valueChangeListener</code> (ex. with Richfaces):</p> <pre><code>&lt;h:selectOneMenu id="countries" value="#{countryBean.selectedCountry}"&gt; &lt;a4j:support event="onchange" actionListener="#{countryBean.changeCountry}"/&gt; &lt;f:selectItems value="#{countryBean.countries }" /&gt; &lt;/h:selectOneMenu&gt; </code></pre> <p>Finally, regarding the difference between the <code>valueChangeListener</code> attribute and <code>&lt;f:valueChangeListener&gt;</code> is that the first binds a Java method (<code>#{myBean.myMethod}</code>), while the second binds a Java class (<code>type="com.foo.MyListenerClass"</code>) which implements the <code>ValueChangeListener</code> interface. So the second one could be more generic than the first one...</p>
 

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