0

I have multiple dropdown lists using Primefaces 5.0. As you select an option in the first drop down list (in this case a country) then it should grab the cities in that country and populate the city drop down list. However, I have code to populate my cities drop down list from my database using the country but the method is 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 all my getters and setters, but that system.out is never called. So irrespective if it is going to call the right cities in that country it never gets there.

Matt
  • 59
  • 9

1 Answers1

0

Your input elements and p:ajax need to be nested within an h:form, otherwise no data is submitted on POST. actionListeners sometimes require access to the submitted value (either through the method parameters or an ActionEvent), so it appears listeners will not be called if the submitted value is not available.

Hint: You can check your browser's developer tools to see what is actually submitted.

See also


You seem to still be having some trouble, so here is a complete example, slightly modified from your 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 that I deleted the class attribute from the commandButton. class is not a 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");
    }
}
DavidS
  • 5,022
  • 2
  • 28
  • 55
  • I'm still going through your links but it was already nested in an h:form. Sorry I omitted that. Any ideas? – Matt Jul 17 '15 at 18:33
  • Those links probably won't help you, @Matt. I just provided them for extra information. – DavidS Jul 17 '15 at 18:37
  • I'm going to post a complete example in a minute, @Matt. – DavidS Jul 17 '15 at 18:38
  • I posted a complete example, @Matt. If you still have trouble, I suggest starting from scratch and creating an MCVE because the code you posted plus a form works for me (minus the `commandButton` bit mentioned above). – DavidS Jul 17 '15 at 19:02
  • Alright, I tried implementing what you did, but a) using the item labels causes an issue where each item has all the values in the list. And b - it still does not call the method as the system.out does not run. – Matt Jul 17 '15 at 19:05
  • I will post the whole class of each now for you in a new answer. – Matt Jul 17 '15 at 19:06
  • @Matt, there's a good chance the problem is somewhere else in the file. Instead of trying to modify the whole thing, start _small_. Get what I posted working _without any addtions_. See for yourself that the action listener is called, and then build up. (For starters, I've never seen anyone embed the entire page content inside an `f:view`.) – DavidS Jul 17 '15 at 19:11
  • please see my answer I posted below this @DavidS if you do not see anything there then I will redo the whole class. – Matt Jul 17 '15 at 19:15
  • Outside of the temporarily nested forms and use of `f:view`, I do not see anything unusual. It's usually not fruitful to spend a great deal of time poring over a large file looking for errors; it's often more productive to just start over. Also, people are less likely to help you if you do not create an MCVE. Your original posted question is not an MCVE because it does not demonstrate the problem: your code works for me! – DavidS Jul 17 '15 at 19:45
  • Okay, thanks @DavidS. I will rewrite the classes and see how that works. Thank you for all your help. My team member and I were talking about this now and one of our other classes does a similar thing but that works. So it is just starting to get frustrating, I think that starting over will be the best bet. – Matt Jul 17 '15 at 19:52