Primefaces valueChangeListener or <p:ajax listener not firing for p:selectOneMenu

J&#229;cob picture Jåcob · Feb 19, 2013 · Viewed 181.7k times · Source

I am using Primefaces 3.4.2.

I have the following in my JSF page

<p:selectOneMenu id="emp" value="#{mymb.emp.employeeName}" 
        valueChangeListener="#{mymb.handleChange}" 
        required="true"
        style="width: 150px;">
    <f:selectItem noSelectionOption="true" 
            itemLabel="Please  Select"/>
    <f:selectItems value="#{mymb.employeeList}" var="emp"
            itemLabel="#{emp.employeeName}"
            itemValue="#{emp.employeeNumber}"/>
    <p:ajax update="sublist"/>
</p:selectOneMenu>

and in ManagedBean

public void handleChange(ValueChangeEvent event){  
    System.out.println("here "+event.getNewValue());
}

The problem is valueChangeListener is not firing, i.e. handleChange method is not getting invoked. I tried with the following, but it is not working either.

<p:ajax update="sublist"  listener="#{mymb.handleChange}" />  

Separate JSF page:

<ui:composition template="/templates/layout.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
    <ui:define name="content">
        <h:head>
        </h:head>
        <h:body>
            <h:form id="form">                      
                <p:panelGrid columns="6">
                    <h:outputLabel value="Employees" for="employees" />
                    <p:selectOneMenu id="employees"
                            value="#{mymb.employeesList}" 
                            required="true">
                        <f:selectItems value="#{mymb.employeesList}" var="emp"
                                itemLabel="#{emp.employeeName}" />
                        <p:ajax listener="#{mymb.handleChange}"   />  
                    </p:selectOneMenu>                  
                </p:panelGrid>
            </h:form>
        </h:body>
    </ui:define>
</ui:composition>

Answer

Mr.J4mes picture Mr.J4mes · Feb 19, 2013

If you want to use valueChangeListener, you need to submit the form every time a new option is chosen. Something like this:

<p:selectOneMenu value="#{mymb.employee}" onchange="submit()"
                 valueChangeListener="#{mymb.handleChange}" >
    <f:selectItems value="#{mymb.employeesList}" var="emp"
                   itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeID}" />
</p:selectOneMenu>

public void handleChange(ValueChangeEvent event){  
    System.out.println("New value: " + event.getNewValue());
}

Or else, if you want to use <p:ajax>, it should look like this:

<p:selectOneMenu value="#{mymb.employee}" >
    <p:ajax listener="#{mymb.handleChange}" />
    <f:selectItems value="#{mymb.employeesList}" var="emp"
                   itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeID}" />
</p:selectOneMenu>

private String employeeID;

public void handleChange(){  
    System.out.println("New value: " + employee);
}

One thing to note is that in your example code, I saw that the value attribute of your <p:selectOneMenu> is #{mymb.employeesList} which is the same as the value of <f:selectItems>. The value of your <p:selectOneMenu> should be similar to my examples above which point to a single employee, not a list of employees.