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

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>

This question is related to jsf jsf-2 primefaces

The answer is


My problem were that we were using spring securyty, and the previous page doesn't call the page using faces-redirect=true, then the page show a java warning, and the control doesn't fire the change event.

Solution: The previous page must call the page using, faces-redirect=true


Try using p:ajax with event attribute,


<p:ajax listener="#{my.handleChange}" update="id of component that need to be rerender after change" process="@this" />



import javax.faces.component.UIOutput;
import javax.faces.event.AjaxBehaviorEvent;

public void handleChange(AjaxBehaviorEvent vce){  
  String name= (String) ((UIOutput) vce.getSource()).getValue();
}

this works for me:

It can be used inside the dialog, but the dialog canĀ“t be inside any componet such as panels, accordion, etc.


The valueChangeListener is only necessary, if you are interested in both the old and the new value.

If you are only interested in the new value, the use of <p:ajax> or <f:ajax> is the better choice.

There are several possible reasons, why the ajax call won't work. First you should change the method signature of the handler method: drop the parameter. Then you can access your managed bean variable directly:

public void handleChange(){  
  System.out.println("here "+ getEmp().getEmployeeName());
}

At the time, the listener is called, the new value is already set. (Note that I implicitly assume that the el expression mymb.emp.employeeName is correctly backed by the corresponding getter/setter methods.)


All can be defined as in f:ajax attiributes.

i.e.

    <p:selectOneMenu id="employees" value="#{mymb.employeesList}" required="true">
         <f:selectItems value="#{mymb.employeesList}" var="emp" itemLabel="#{emp.employeeName}" />
         <f:ajax event="valueChange" listener="#{mymb.handleChange}" execute="@this" render="@all" />
    </p:selectOneMenu>

event: it can be normal DOM Events like click, or valueChange

execute: This is a space separated list of client ids of components that will participate in the "execute" portion of the Request Processing Lifecycle.

render: The clientIds of components that will participate in the "render" portion of the Request Processing Lifecycle. After action done, you can define which components should be refresh. Id, IdList or these keywords can be added: @this, @form, @all, @none.

You can reache the whole attribute list by following link: http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/ajax.html


Another solution is to mix valueChangeListener, ajax and process:

<p:selectManyCheckbox id="employees" value="#{employees}" columns="1" layout="grid" valueChangeListener="#{mybean.fireSelection}"   >
    <f:selectItems var="employee" value="#{employeesSI}" />
    <p:ajax event="valueChange" immediate="true" process="@this"/>
</p:selectManyCheckbox>

Method in mybean is just :

public void fireSelection(ValueChangeEvent event) {
    log.debug("New: "+event.getNewValue()+", Old: "+event.getOldValue());
}

Like this, valueChangeEvent is very light !

PS: Works fine with PrimeFaces 5.0


Examples related to jsf

Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes Target Unreachable, identifier resolved to null in JSF 2.2 Execution order of events when pressing PrimeFaces p:commandButton selectOneMenu ajax events The entity name must immediately follow the '&' in the entity reference java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config Primefaces valueChangeListener or <p:ajax listener not firing for p:selectOneMenu How does the 'binding' attribute work in JSF? When and how should it be used?

Examples related to jsf-2

Execution order of events when pressing PrimeFaces p:commandButton How to use if, else condition in jsf to display image Primefaces valueChangeListener or <p:ajax listener not firing for p:selectOneMenu How does the 'binding' attribute work in JSF? When and how should it be used? Difference between h:button and h:commandButton What is the JSF resource library for and how should it be used? List of <p:ajax> events Can I update a JSF component from a JSF backing bean method? How to provide a file download from a JSF backing bean? How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable

Examples related to primefaces

Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes Execution order of events when pressing PrimeFaces p:commandButton selectOneMenu ajax events Primefaces valueChangeListener or <p:ajax listener not firing for p:selectOneMenu List of <p:ajax> events Can I update a JSF component from a JSF backing bean method? How to remove border from specific PrimeFaces p:panelGrid? How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar" How to set width of a p:column in a p:dataTable in PrimeFaces 3.0?