Ajax – How to use JSF(Primefaces) ajax Listener

ajaxjsfprimefaces

I am having problem in using the ajax tag of the listener to invoke a method in the managed bean. The following code does not work. Although the ajax is invoking the JavaScript method view.show(), it does not invoke the managedbean method in the listener property.

<p:dataTable id="dataTable" var="customersFeedback"
  value="#{customersFeedbackController.allReadFeedbacks}"  
  widgetVar="reservationTable"
  selectionMode="single" 
  selection="#{customersFeedbackController.customersFeedback}" 
  rowKey="#{customersFeedback.id}">

   <p:ajax event="rowSelect" update=":viewFeedbackForm:viewDlg" listener="#
     {customersFeedbackController.setMessageAsRead()}" oncomplete="view.show()"/>
</p:dataTable>

Here is my managedbean. The update method is simply a method to update the CustomerFeedback object in the database.

@ManagedBean(name = "customersFeedbackController")
public class CustomersFeedbackController implements Controller<CustomersFeedback> {

    private CustomersFeedback customersFeedback = new CustomersFeedback();
    // other fields here

    @Override
    public void update() {
        customersFeedbackDao.update(customersFeedback);
    }

    public void setMessageAsRead() {
        customersFeedback.setStatus("Read");
        this.update();
        System.out.println("Method Executed"); // Just a test
    }

    // Other methods here

}

am I doing something wrong? If there is, feel free to correct me. Thanks.

Best Solution

I observed that in your code for ajax you used

listener="#
     {customersFeedbackController.setMessageAsRead()}" 

instead of this use following code:

listener="#
     {customersFeedbackController.setMessageAsRead}"

brackets are not needed to call function from backing bean.

also add SelectEvent to your backing bean method. like: public void setMessageAsRead(SelectEvent event){}

refer: http://www.primefaces.org/showcase/ui/ajaxifyAction.jsf