jsf - Primefaces 5.0 and Ajax. Listener method not being called -


i have multiple dropdown lists using primefaces 5.0. select option in first drop down list (in case country) should grab cities in country , populate city drop down list. however, have code populate cities drop down list database using country method never called.

my region.xhtml:

<h:form> <p:tab title="region selection"  rendered="true">                                         <p:panel header="region information">         <h:panelgrid columns="2" columnclasses="label, value">             <h:outputtext value="country" />             <p:selectonemenu id="country" value="#{rosterbean.selectedcountry}">                 <p:ajax listener="#{rosterbean.oncountrychange()}" update="state" />                 <f:selectitem itemlabel="select country" itemvalue="" noselectionoption="true" />                 <f:selectitems value="#{rosterbean.countries}" />             </p:selectonemenu>              <h:outputtext  value="state/ province" />             <p:selectonemenu id="state" value="#{rosterbean.selectedstate}">                 <p:ajax listener="#{rosterbean.onstatechange}" />                 <f:selectitem itemlabel="select state" itemvalue="" noselectionoption="true" />                 <f:selectitems value="#{rosterbean.states}" />             </p:selectonemenu>              <h:outputtext value="city" />             <p:selectonemenu  id="city" filter="true" filtermatchmode="contains" >                 <f:selectitem  itemlabel="johannesburg" itemvalue="0" noselectionoption="true" />                 <f:selectitem itemlabel="pretoria" itemvalue="1" noselectionoption="true" />             </p:selectonemenu>           </h:panelgrid>     </p:panel> </p:tab> </h:form> 

my bean:

@viewscoped @managedbean public class rosterbean implements serializable {  public void oncountrychange() { system.out.println("updating state"); } } 

i have got getters , setters, system.out never called. irrespective if going call right cities in country never gets there.

your input elements , p:ajax need nested within h:form, otherwise no data submitted on post. actionlisteners require access submitted value (either through method parameters or actionevent), appears listeners not called if submitted value not available.

hint: can check browser's developer tools see submitted.

see also


you seem still having trouble, here complete example, modified own:

<?xml version='1.0' encoding='utf-8' ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"       xmlns:h="http://java.sun.com/jsf/html"       xmlns:p="http://primefaces.org/ui"       xmlns:f="http://java.sun.com/jsf/core">     <h:head>     </h:head>     <h:body>                                            <h:form>             <p:panel header="region information">                 <h:panelgrid columns="2" columnclasses="label, value">                     <h:outputtext value="country" />                     <p:selectonemenu id="country" value="#{rosterbean.selectedcountry}" >                         <p:ajax listener="#{rosterbean.oncountrychange()}" update="state" />                         <f:selectitem itemlabel="select country" itemvalue="" noselectionoption="true" />                         <f:selectitem itemlabel="country 1" itemvalue="1" />                     </p:selectonemenu>                      <h:outputtext  value="state/ province" />                     <p:selectonemenu id="state" value="#{rosterbean.selectedstate}" >                         <p:ajax listener="#{rosterbean.oncountrychange}" />                         <f:selectitem itemlabel="select state" itemvalue="" noselectionoption="true" />                         <f:selectitem itemlabel="state1 1" itemvalue="1" />                     </p:selectonemenu>                  </h:panelgrid>             </p:panel>         </h:form>         <p:commandbutton value="next"/>     </h:body> </html> 

(note deleted class attribute commandbutton. class not valid attribute on commandbutton.)

rosterbean.java

import javax.faces.bean.managedbean; import javax.faces.bean.viewscoped; @managedbean @viewscoped public class rosterbean {      public string selectedcountry;     public string selectedstate;      public string getselectedcountry() {         return selectedcountry;     }      public void setselectedcountry(string selectedcountry) {         this.selectedcountry = selectedcountry;     }      public string getselectedstate() {         return selectedstate;     }      public void setselectedstate(string selectedstate) {         this.selectedstate = selectedstate;     }      public void oncountrychange() {         system.out.println("updating state");     } } 

Comments